Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - Unit test, Mock?

The builtin unit test generator(VS) for the target classes should that be used or should I learn myself how to write a unit test? And whats this "Mock" thing? I hear it over and over but none cares to give a good explanation.

Thanks in advance.

like image 955
ebb Avatar asked Sep 13 '10 16:09

ebb


1 Answers

You need to learn to write unit tests on your own. Start off on the right foot with good comprehension of terminology that many people make mistakes regarding:

Unit test: Testing a single unit of code, very small atomic test.

Integration test: Testing multiple units of code integrated together, to go through the different layers and ensure they are using eachother correctly. These should be done after unit tests have verified the individual units work independently. Many people mistakenly refer to these as unit tests.

Built Verification Test: Testing the built product by deploying it and running tests that will interact with it in the manner a user would. Also mistakenly referred to as unit tests frequently. These are the largest most full featured tests and often just done manually by testing teams rather than being automated.

Here's the quickstart for MOQ which is a mocking framework: https://github.com/Moq/moq4/wiki/Quickstart

Mocking is the act of taking a small piece of code that may depend on other things, mocking those other things up so that you can control the circumstances surrounding the piece of code you want to test.

The purpose of mocking is atomicity in tests. It allows you to test just the individual piece of code you want, without having it's tests affected due to bugs in dependent code pieces. Also mocking gives you the ability to fabricate a variety of scenarios to test the edge cases of each piece of code.

Mocking is generally purposed for creating boundaries around the target code in unit tests, though it's not uncommon to use it in integration tests too for fabricating a resource that acts as the seed to the integrated code chain you're targeting.

like image 154
Jimmy Hoffa Avatar answered Sep 18 '22 08:09

Jimmy Hoffa