NUnit, Unity Dependency Injection, MOQ and Private Fields
loading...
I had an interesting puzzle to solve this week that I thought I would share with you in case someone else is looking for a similar solution.
There was some code that I needed to test that ultimately called into the database. Since this is a UNIT test and all I was interested in testing was one specific function and the state of one specific field in another object, I had neither the need, nor the desire, to let that call to the database happen. Since MOQ is my mocking framework of choice, I wanted to mock out the database object the method was using so that it would return whatever was expected without actually calling down into the database.
There were several problems. The first was that the database object is a private field in the class I was testing and it got created by the constructor. Second, the code that needed to use the database object is passing the object by reference (using the “ref” keyword) so that I could no setup my class that needed that object to ju
Comments