Moving from RhinoMocks to Moq

RhinoMocks is a framework for creating mocked instances, that can be used within unit tests. Since the framework is not to be maintained anymore (last version was in 2010), you can’t use RhinoMocks for example within .Net Standard or .Net Core projects.

A project I was working on was having the same struggle; we had quite a large set of unit tests that where using RhinoMocks. For several reasons, we decided to migrate our projects to .Net Core, which made it impossible to use RhinoMocks anymore. We decided to switch to the Moq framework. In this blog I summed up the most used RhinoMocks constructions and how to port them to Moq.

Read More…
Posted in C#, TDD, Unit testing at June 29th, 2020. No Comments.

Difference between unit testing and component level testing

There are different levels of testing a programmer can do, the must read Code Complete differs the following types of tests: unit testing, component testing, integration testing, regression testing and system testing. The terms unit testing and component testing are quite confusing to most people.

Within unit testing you use stubs and mocks to test directly your code. The code is tested in isolation. Typical things you want to stub are hardware, networks or databases.

Component level testing is situated at a higher level. You remove the stubs and mocks and test your code against the real thing. So you remove the stubbed hardware interface and put a real device to the test.

Posted in TDD at July 21st, 2013. No Comments.

The difference between mocks and stubs

Within the field of unit testing, we use “mock objects” (see also one of my previous posts) and “stubs” to help us testing our classes. For example when having a class which writes directly to a piece of hardware, and you want to write a unittest for it, you have to create some stand-in class which operates like this device. This is needed since the device isn’t really there when running the tests on a build machine. Also, we probably want to force our device in some state or want to get rid of time constraints. Since many people actually don’t know the difference between a stub and a mock, you can take the following rule of thumb about the difference between mocks and stub:

  • With mocks you can control the flow of your object. You can for example call a mock instance which will react the same as if you would interface with a real piece of hardware but without the need of the hardware of having to deal with time constraints.  With a mocking framework you are be able to influence the behavior of the object you have mocked.
  • Stubs normally return a default value. For example they return always true when calling a method which has a return value.

So in short: if you won’t want to control the behavior of your object, you want to write a stub. Otherwise you write a mock object for your unittest.

Posted in TDD at February 17th, 2013. 1 Comment.

Mock objects

An often used technique within unit testing is mocking of objects. Mocking means that you create an instance of an object, which responds exactly as you want. This comes in really handy when you for example want to unit test a piece of code which interacts with some piece of hardware. By simply mocking the hardware you can “simulate” the hardware in order to test your code which access this hardware.
NUnit has by default a framework for mocking objects. This framework is also used by unit testing the nunit framework. When you have an interface and don’t need to implement all methods which are available within this interface, you can simply create a mock object from this interface by using “DynamicMock” within the NUnit framework:
DynamicMock myExampleMock = new DynamicMock(typeof(IMyExample)); // Creates a mock object from the defined interface
myExampleMock.ExpectAndReturn(“AmethodName”, // the method name
“abc” // return value
Null  // expected arguments);
IMyExample myExampleInstance = (IMyExample)securityTokenServiceMock.MockInstance;
This will create an instance of an object which implements the given interface. With the “ExpectAndReturn” you can define methods on your mockup and implement how they will react on performed method operations. For example in the example above, when you call “AmethodName” on your myExampleInstance, the string “abc” will be returned.
To use the mockup framework, beside adding the nunit.framework.dll, you also have to add the nunit.mock.dll assembly to your project.
Posted in C#, TDD at July 2nd, 2012. 1 Comment.