NUnit, C#, Mock objects

Caporegime
Joined
18 Oct 2002
Posts
29,493
Location
Back in East London
Anyone use NUnit on C#? Tried using it's mock framework for the first time today, and it seems I have to mock an interface, can't mock a class.. is this right?

Going by an example I found on the web, I try to instantiate like so:
Code:
DynamicMock mock = new DynamicMock(typeof(ClassIAmMocking));
but that spits an exception, complaining it's not the right type?

I hate type strict languages :(
 
...it seems I have to mock an interface, can't mock a class.. is this right?

Yep, I believe that's correct. I think the mocking framework within NUnit is rather lightweight, basically just taking the view that if you have a concrete implementation of a class then you might as well just use that in your tests rather than mocking its public interface. Obviously this isn't helpful when the concrete class is tricky to instantiate due to its dependancies and/or behaviour.

Welcome to .NET by the way ;)
 
at work we use Rhino Mocks which seems to be better documented although I think most of them have a steep learning curve once you get in there and have a play for a while the ball drops and your start making sense of things.
 
Ok, ta for info. I've had possibly more experience with mocks and testing than most developers tbh, I am the lead contributor to SSpec and BDD for Smalltalk, also a main contributor to many xUnit and xSpec frameworks, it just seems odd that NUnit expects an interface, when it should expect an interface or a class.

I'll look into Rhino Mocks when back at work, ta :)
 
Last edited:
Back
Top Bottom