Auto Mocking using JustMock
Posted
by mehfuzh
on ASP.net Weblogs
See other posts from ASP.net Weblogs
or by mehfuzh
Published on Mon, 22 Oct 2012 01:22:28 GMT
Indexed on
2012/10/22
23:01 UTC
Read the original article
Hit count: 480
Auto mocking containers are designed to reduce the friction of keeping unit test beds in sync with the code being tested as systems are updated and evolve over time.
This is one sentence how you define auto mocking. Of course this is a more or less formal. In a more informal way auto mocking containers are nothing but a tool to keep your tests synced so that you don’t have to go back and change tests every time you add a new dependency to your SUT or System Under Test.
In Q3 2012 JustMock is shipped with built in auto mocking container. This will help developers to have all the existing fun they are having with JustMock plus they can now mock object with dependencies in a more elegant way and without needing to do the homework of managing the graph.
If you are not familiar with auto mocking then I won't go ahead and educate you rather ask you to do so from contents that is already made available out there from community as this is way beyond the scope of this post.
Moving forward, getting started with Justmock auto mocking is pretty simple. First, I have to reference Telerik.JustMock.Container.DLL from the installation folder along with Telerik.JustMock.DLL (of course) that it uses internally and next I will write my tests with mocking container. It's that simple!
In this post first I will mock the target with dependencies using current method and going forward do the same with auto mocking container.
In short the sample is all about a report builder that will go through all the existing reports, send email and log any exception in that process.
This is somewhat my report builder class looks like:
Reporter class depends on the following interfaces:
- IReporBuilder: used to create and get the available reports
- IReportSender: used to send the reports
- ILogger: used to log any exception.
Now, if I just write the test without using an auto mocking container it might end up something like this:
Now, it looks fine. However, the only issue is that I am creating the mock of each dependency that is sort of a grunt work and if you have ever changing list of dependencies then it becomes really hard to keep the tests in sync. The typical example is your ASP.NET MVC controller where the number of service dependencies grows along with the project.
The same test if written with auto mocking container would look like:
Here few things to observe:
- I didn't created mock for each dependencies
- There is no extra step creating the Reporter class and sending in the dependencies
- Since ILogger is not required for the purpose of this test therefore I can be completely ignorant of it. How
- cool is that ?
Auto mocking in JustMock is just released and we also want to extend it even further using profiler that will let me resolve not just interfaces but concrete classes as well. But that of course starts the debate of code smell vs. working with legacy code. Feel free to send in your expert opinion in that regard using one of telerik’s official channels.
Hope that helps
© ASP.net Weblogs or respective owner