today I had a bizarre experience using nhibernate.
I’m writing an audit trail system on the top of nhibernate, that have to satisfy certain regulatory requisites.
Of course I’ve a lot of test that proves that my work is proceeding in the right direction. But this morning I’ve added few line of code to introduce a new feature that at the end requires a “references” clause in the nhibernate mapping.
Suddenly some of the test went broken.
I’ve tought that I’ve wrote something wrong in my code so I’ve run the test in debug mode… and guess what? all the test pass…
I’ve then tought that there was some particular situation (maybe some file locked or god-knows-what-like situation that have caused the transient… or what I’ve supposed to be a transient) and then everything was returned normal.
so I’ve wrote other few lines of code and run the test again and BANG! red again…
this time I’ve started investigating more, I’ve lost the whole morning
the symptoms were the followings.
1) if I RUN the test form the test list windows of visual studio, some of the test (and always the same tests) systematically fails.
2) if I DEBUG the failing tests from within the test result window or the test list window the exact same test pass
3) from the moment when the test PASS in DEBUG, I could RUN the test again from the test result windows, and they KEEP passing
4) if for any reason I RUN again the same exact test from the test list window they start failing again.
the error message when they fail was something like “the property method get_Xyz could not be found”
and in fact that was the property I was working on.
at the end I’ve discovered the cause.
the Code coverage instrumentation was activated on the assembly I was editing.
for a reason that nobody will ever know, visual studio have cached somewhere (probably over the rainbow) something about how to instrument the assembly I was working on.
no mater how I’ve tried to delete the BIN and OBJ and TestResults of all the involved projects… they keep presenting the same behavior.
Guess what? this visual studio magical cache went for another unknown reason out of date.
and the result (I’ve figured out) was that out of date code was keep injected in my code causing all the sort of the bad things… but only if I RUN the test.
Luckily the DEBUG of the test doesn’t inject (outdated) the instrumentation code.
Once understood that the code coverege was the cause it was easy to solve.
just shut the code coverege down, recompiled everything, runned the code => green, debugged the code=> green!
after this I’ve turned on the code coverage again and tested the tests another time: green again! YAY!
So, if your test starts to become reds without any reason, give a look at the code coverage