Moq Quick Reference for Rhino Mocks Users

Behold! A quick reference guide for Rhino Mocks (AAA syntax) users making the switch to Moq.

Rhino MocksMoq
MockRepository.GenerateMock<T>new Mock<T>
MockRepository.GeneratePartialMock<T>new Mock<T> { CallBase = True }
.Stub(…).Return(…)
.Stub(…).WhenCalled(…)
.Stub(…).Do(…)
.Setup(…).Returns(…)
.Expect(…).Return(…)
.Expect(…)
.Setup(…).Returns(…).Verifiable()
.Setup(…).Verifiable()
Arg<T>.Is.AnythingIt.IsAny<T>()
Arg<T>.Matches(…)It.Is<T>(…)
Arg<T>.Is.Equal(foo)It.Is<T>(x => x == foo)
.AssertWasCalled(…).Verify(…)
.AssertWasNotCalled(…).Verify(…, Times.Never)
.VerifyAllExpectations().Verify()

In addition to the syntax translations listed in the table above, a key difference is that you use the instantiated mock object via the mock’s Object property. For example:

var mock = new Mock<SomeClass>();
SomeMethod(mock.Object);

Author: Adam Prescott

I'm enthusiastic and passionate about creating intuitive, great-looking software. I strive to find the simplest solutions to complex problems, and I embrace agile principles and test-driven development.

Leave a comment

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 )

Google photo

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

Twitter picture

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

Facebook photo

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

Connecting to %s