Monday, August 31, 2009

NCover – Empty Coverage.xml

I spent a half day investigating an empty coverage.xml file generated by NCover console. Finally I had no idea at all.

Accidentally I recognized that the .pdb was not in the directory. After generating the .pdb file into the directory, the expected results appeared.

Sunday, August 30, 2009

BSOD 0x0000003b on Windows 7 x64

I got a regular Blue Screen of Death after exiting a 3D application. I have the most recent drivers, so they couldn’t be the problem. The error code was 0x0000003b.

Workaround:
In my case Outpost Firewall was the source of the BSODs. Turn off while you are playing and there will be no BSODs.

Sunday, August 23, 2009

NUnit on 64 bit

I have a Windows 7 x64 version on my computer. I’m developing a project that uses Sql CE 3.5. While SqlCE depends on x86 binaries, I had to set my .NET project settings to compile code to x86 architecture. I forced .NET to load the assemblies as x86 binaries.

I wrote NUnit tests for my project. When I tried to run them, I got a huge exception:

TESTNAME:
System.BadImageFormatException : Could not load file or assembly 'ASSEMBLY' or one of its dependencies. An attempt was made to load a program with an incorrect format.

NUnit.exe is an architecture-independent IL code, so it runs as an x64 code on my machine. My project is an x86 code, so NUnit couldn’t load the DLLs into it’s appdomains.

Solution:
Run NUnit-x86.exe because that is a 32-bit assembly.

Thursday, August 20, 2009

NMock 2 vs Rhino Mocks

I am writing unit tests for legacy code. A former programmer started using NMock 2. The first problem I run into was abstract classes. NMock 2 can’t mock abstract classes. So I moved to Rhino Mocks.

The basic ideas in both mock frameworks are the same. Here is a comparison chart:

Feature

NMock 2

NMock logo

Rhino Mocks

Rhino Mocks logo

Mock interfaces + +
Create stubs + +
Mock abstract classes - + (Partial Mocks)
Mock (implemented) classes + +
Strict replay + +
Loose replay - +
Define expectations + +
Ordered expectations + +
Record-Playback - (implicit) +
Expectation with method parameters - (method as string) +
Generic framework methods + +
Record-Playback syntax (with using block) - +
Clear existing expectations from repository + -
Documentation sloppy detailed
API documentation none detailed
Intellisense - +

Replay modes

  • Strict replay: Recorded methods are accepted as valid, any other method calls cause an exception.
  • Loose replay: Unexpected method calls return null or zero, no exception.
  • Partial mocks: Mocks only the requested methods.

Record-playback

Every single mocking framework uses this method. First you define your expectations (record) and execute them (playback) after that. NMock 2 covers it from the users. Rhino Mocks uses it explicitly.

Expectation with method parameters

In case of NMock 2 you define an expectation as Expect.Once.On(aMock).Method( “MethodName” )… You do it in Rhino Mocks as Expect.Call( dependency.MethodName() )… It means that NMock 2 won’t detect code changes and will cause runtime exceptions; Rhino Mocks will cause compile time exceptions.

Record-Playback syntax

Rhino Mocks supports using(mocks.Record()){…} using(mocks.Playback()){…} syntax. It makes possible to separate expectations from test code execution, making your test cases much more structured and neat.

Clear existing expectations from repository

It sounds a good idea at first. You can erase all the expectations from the current mock repository. It can be a nightmare in practice. One of the main goals of unit testing is separating test cases from each other. When you have to clear the mock repository to be able to define new mock and expectations, you can be in big trouble. Please avoid it!

Documentation

NMock 2 has just tutorials and cheat sheets. Even the downloaded code has no XML comments. It can make your work really hard when you have to face complicated SUT (System Under Test) code. Rhino Mocks has excellent documentation with a lot of examples.

Conclusion

If you are a newbie to unit testing and mocking you can start with NMock 2. It’s easy to start with, understand and use. If you have to work with legacy code, on big project or any serious, Rhino Mocks is a must.

Link: Rhino Mocks
Link: NMock 2