views:

77

answers:

1

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 god damn explanation..

Thanks in advance.

+3  A: 

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: http://code.google.com/p/moq/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.

Jimmy Hoffa
Should those "mocks" be saved in a class for themself or are they just temporary to test some code out and then remove the mock(s) afterwards?
ebb
@ebb: That is an old school way of doing it and works fine, but that's where mocking frameworks come in, they allow you to mock up objects without actually creating the mock class to use. All mocking however depends on having a polymorphic relationship with the objects you're pretending to be. So you need an interface or base class to share, or virtual methods to override on the objects you would pretend to be.
Jimmy Hoffa
What should I learn first.. Mocking or Unit testing?
ebb
@ebb - I'd suggest learning basic unit testing first for bits of code that do not have dependencies on external systems like web services and databases. Once you've done some of that, you can learn how to unit test code which interacts with external systems by mocking them.
Alex Humphrey
And +1 @Jimmy, especially for differentiating between unit and integration tests. Far too many people write integration tests convinced that they're unit tests and are put off by TDD because of it. Unit testing is more than just a way of testing your code - it drives modular design.
Alex Humphrey