This is an overview of some testing possibilities in C.Net with Visual Studio.
I suggest you install NCrunch and NUnit. Starting with that, you will be able to have all the testing framework you need.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
It’s that easy! Also, I suggest using FluentAssertions, for some easy to read assertions like:
Now, unit tests are not often unit tests without some mocks. I’ve looked at two frameworks :
- Rhino Mocks
I personally prefer Moq at it looks a bit more like Mockito (Java) to me, but both are quite easy to use. Here’s an example with Rhino Mock :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
As you noticed, we have to use a MockRepository to create mocks. Also, you have to say ReplayAll for the stubs or the Expect to work. The Expect class creates what you need to verify if a method was called during the test. Stub method only creates the stub without any care of how many times the method will be called.
UPDATE: There’s also another way of coding with RhinoMock. You just have to generate a Stub instead of a Mock. That way, the synthax will require you to respect a “Given-When-Then” test structure instead of a “Configure-Test-Replay” structure, which often hides the assertion at the top of a test’s code.
Here’s an example with Moq :
1 2 3 4 5 6 7 8 9 10
I prefer Moq because you don’t have to create a MockRepository, you just have to say setup and verify the number of calls if you want after. This verification of the number of times is also quite easy to remember.
I hope it helped!
Oh, and there is one flaw due to the language’s origins (C++) : you have to add the virtual keyword to the stubbed methods for both tools. There is one tool that does not require that for C#, but it’s not a free framework so I have not tried it.