Wednesday, June 16, 2010

RhinoMocks - Method Signature & Null Reference Exception

OK, so it took me a while to figure this one out...

I would compile my project, would work fine, but when I ran the test, I would get "Object reference not set to the instance of an object" exception. I thought for the longest time it was my Mock object, but it wasn't.

The cause was because my method signature that I was stubbing was different than what I defined in my stub. So reflection couldn't find the method I was telling Rhino to stub.

Another interesting tidbit, the way I arranged my Given statements, I set an expectation on a Repository that method Get(int) would be called, then later in the Given I set the value of int. The mock was not prepared to handle "any" int, but only the one I set with the Expectation. Once I moved the assignment of int above the Mock Repository, it worked as expected.

As a side note, the order of the Given statements in the Feature determine the order the methods in the Spec are run. Interesting...

No comments:

Post a Comment