the Musing

12 Are You Mocking Me?!

Published: 1/10/2010

Mark and Clark talk about the use of mocks and stubs for unit testing. They define each and then talk about when they are best used. Tips and tricks include data driven testing using Excel and using Ping with IPV6 and IPV4.

Show Info

Stubs and Mocks help you with unit testing by providing a mechanism for simulating a resource (database, file system, network or web interface, etc.) and allowing your test to isolate the method being tested.  They both give you a way to make method calls, pass parameters and get data back.  The big difference is that stubs just allow a method call and will then return data back.  This lets your method under test execute and pass back values to the unit test.  The test can then determine success.  This is state.

A mock is similar but you set up expectations around what parameter values should be allowed and what calls should be made.  At the end of the unit test, you ask the mock if it met all of the expectations.  At that point, you know in your unit test whether or not your test succeeded.  This is behavior.

Some (Certainly not all-inclusive) Mocking Frameworks

Check out this blog post that talks in more detail about mocks and stubs and has some sample code to illustrate some of the things we talked about.

Tips and tricks

Using Excel for Rule-Based Data Driven Testing with MSTest

Check out this blog post that describes this in detail.

Ping on a system using IPV6 so that it uses IPV4

You may run across a need to use Ping on a network configured to use IPV6.  That's great but trying to type in an IPV6 IP address can be daunting because of its size.  Or, you may know the IPV4 (#.#.#.#) and just want to type it in.  Ping has a switch -4 that allows you to work specifically with IPV4 addresses.


comments powered by Disqus