(Not so much) fun with Rhino Mocks

After creating some unit tests, I ran into a problem where the TestInitialization method was failing with either one of two errors.

The test initialization was performing something similar to:

[TestInitialize]
public void Initialize()
{
	myMock = MockRepository.GenerateMock<IMyInterface>();
	myMock.Stub(m => m.NextValue()).Return(6).Repeat.Once();
	myMock.Stub(m => m.NextValue()).Return(7).Repeat.Once();
	myMock.Stub(m => m.NextValue()).Return(9);
}

The first error I might get on the GenerateMock would be:
    System.NullReferenceException occurred
      Message=Object reference not set to an instance of an object.
      Source=mscorlib

Or, on one of the Stub calls I might get:
  System.InvalidOperationException occurred
  Message=Previous method ‘IMyInterface.MyMethod();’ requires a return value or an exception to throw.
  Source=Rhino.Mocks

This was a really frustrating problem and it took getting several people reviewing the problem with me before one individual spotted that two unit tests were running at once.  The problem ended up being that using the parallelTestCount=”2″ attribute in the testrunconfig file and the total problem that using this creates.  Trying to execute multiple tests at once would be great except for the fact that MSTest executes multiple parallel tests within the same test class.  Rhino Mocks is also NOT multi thread safe.

So the next time you see these types of errors, check to see if you’re using a parallelTestCount greater than 1.  It might save you a lot of time.

Happy Coding

Advertisement

About JohnHowell

I am a professional software developer with over 20 years of experience. I currently specialize in Microsoft technologies such as VS, TFS, C#, VB.Net, WCF, WPF, WW, etc.
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s