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);