Currently I was asked to create Unit testing for my sample MVC app. Just in my previous post I had mentioned about Unit Testing of controllers. But along with that I had to also create Mock Objects in my application
Dint know where to start from . I started with google J . As we know google is the best in its search results ,it showed me different links to Mock Objects .. Upon reading a bit , I came to a conclusion that “Mock Obkects are nothing but a Mimic or Fake Objects of the real objects we create in our application.” Question was why do we create these mimic object and how would they help me in Unit Testing ..
For getting these answers ,let me illustrate a real time environment where we have an object say object A which depends upon Object B and Object C. As we see in the fig below,there is Object C which depends upon Object D which in turn depends on Object E..This chain of Dependency continues ,which makes it all the more difficult to test on just Object A .
In order to avoid this ambiguity lets create a Mock of obj B and Mock of obj C like the following.
Here we have Mock of B and Mock of C where MockC basically just uses the interface of Object D .
By doing TDD with Mock objects you will produce components which are independent of each other implementation.
Example from my project



No comments:
Post a Comment