Moq Vs Rhino Mocks

Just some rough notes I made coming from a Moq framework and converting to Rhino Mocks:

Asserting a method call

ISomethingRepository mockedSomethingRepository = MockRepository.GenerateMock();

var person = new Person { name = “Bob” };

// Act
var something = new Something(mockedSomethingReporsitory);

// Assert
mockedSomethingRepository.AssertWasCalled(x => x.Save(person));

Asserting a property was set

As above but assert on the property….

mockedSomethingRepository.AssertWasCalled(x => x.WasPersonSaved = true);

Using a Stub to control program flow

Similar to Setup with Moq we’d use a Stub to return the desired flow i.e:

ISomethingRepository mockedSomethingRepository = MockRepository.GenerateMock();

mockedSomethingRepository.Stub(x => x.ValidatePerson(Arg.Is.Anything)).Return(true));

Using a Stub to return the desired set on a property

ISomethingRepository mockedSomethingRepository = MockRepository.GenerateMock();

mockedSomethingRepository.Stub(x => x.IsValid).Return(true));

You can use GenerateStub here but the above setup for the stub keeps things syntactically the same in all mock setup instances.

Using Constraints to assert a method call with specific properties

In the first example we simply checked that a method was called. If we wanted to check the method was called passing in an object with specific properties we would do the following:

var personId = 23;
mockedSomethingRepository.AssertWasCalled(x => x.Save(Arg.Matches(x => x.Id == personId));

Add a Comment

Your email address will not be published. Required fields are marked *