While unit testing a target type, it is likely that the test is failing during object creation because we forgot to include a mandatory configuration file or a line in the constructor is throwing exception because it is making call to some external service or data store which on the other hand requires a little more orchestration to get things going . To better illustrate this, let’s create an entity framework data container either new or from an existing database. Once we have completed the required steps, there will be an entry-point class where we will notice the following default constructor:
The comment clearly shows that by default it will search for a connection string named “NorthwindEntities” in the configuration file. Though this is not an ideal case where it makes the class more production oriented less TDD friendly (if you think it in a TDD purist way) but to avoid this kind of situation the newest release of Telerik JustMock (Free / Commercial) includes a way to mock such case comprehensively. Thus making it flexible for testers who don’t need to include a configuration file or initialize a default service.
Using the JustMock free edition, I can now easily test the above data container without going through any unnecessary configuration hassle:
The key here is the Consturctor.Mocked overload in Mock.Create<T>. By default it is set Constructor.NotMocked as general but incase your constructor is throwing some unknown exception and you have little control over its codebase then this could be just it.
# region Developer’s log:
As the feature is supported in JustMock free edition, it is right to say that there is no profiler involved. Therefore, it is not possible to use FormatterServices in that regard :
If we just open up the NorthwindEntities class in IL dissembler , we will see something like this:
Here on line 4 it is first calling the base constructor which is actually the case for all .net objects. Even If the class has no base, .net framework will still do an instance call to System.Object itself. But it is possible to skip the call during proxy initialization dynamically that framework wont let you do otherwise if the base class has no default constructor. Of course, this wont work with sealed class in that case profiler is the only resort. Also, Silverlight runtime does not allow it but you can still reference Silverlight class library from .net test project to make things work.
Hope that you will find the Constructor.Mocked feature useful. You can download sample the project for this post here EntityFramework01.zip. Finally If you like NuGet, you can also try install-package JustMock to grab the latest bits.