Unitary Tests


Probably you have Heard about Unitary Test but you don’t use it. It’s funny because when you talk about unitary test, everybody knows what are but noone use it… Under my point of view, this is an error. Unitary test allow us to test our application in few minutes and only with a click. Is useful to detect regression errors or error over different databases (if apply), countries, etc.

We are going to add unitary test to our MVP Project. The first step is create a unity test Project and after that we can start to create the methods to test every functionality of our application. We will use RhinoMocks framework, that allow us to test your MVP pattern. RhinoMocks create associations between our interfaces and  the implementations.

1

Now, we can create our test and use RhinoMocks to associate interfaces to his implementation. You can use Unity too but in my case i prefer put directly the implementation through RhinoMock because i can test several implementations for the same interface.

2

We have put the two implementations for our method, so depends which one we call we are going to test the Entity Framework or the list.

3

We have initialized our view through RhinoMocks too and we have used the E.F. service (we will have another one for the list). We call Find event and if we don’t get results we will launch an error). After that we retrieve the first element and we try to search by name. Again if we don’t have any result, we launch an error.

If we test it, we can see that the methods Works fine.

4

As you can image, only clicking or “Run All”, all test start to check and if one of them fails we will have an error. If we change some part of the code and we don’t have the expected results, probably we are going to get an error too (depends the implementation of the test), so is very useful to detect error in the entire application in an  easy way. We can test it at this level or method by method (Model, Service, Presenter, etc.).

You can download the code here.