Moq Quick Reference for Rhino Mocks Users

Behold! A quick reference guide for Rhino Mocks (AAA syntax) users making the switch to Moq. Rhino Mocks Moq 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.Anything It.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 … Continue reading “Moq Quick Reference for Rhino Mocks Users”

Partially Mock Static Classes With Rhino Mocks

One of my favorite unit testing techniques is using partial mocks with Rhino Mocks. This allows me to substitute logic or assert function calls in my unit tests, making it simple to write concise, targeted tests. Here’s a quick example that demonstrates the basic concept. A partial mock is essentially just a derived class that … Continue reading “Partially Mock Static Classes With Rhino Mocks”

Unit Tests: Why I Love ‘Em

Unit tests are one of my favorite things to write as a developer. I simply love them. It’s like a little game I get to play with myself to ensure I’ve covered all possible scenarios and verify that my code is absolutely bulletproof. If I’m doing maintenance work on a project without tests, you better … Continue reading “Unit Tests: Why I Love ‘Em”

Ordered Tests with Rhino Mocks AAA Syntax

Before the AAA syntax was introduced, you would create ordered unit tests by wrapping Expect calls in a using block like so: However, AAA syntax does not require you to instantiate a MockRepository, so how can we do ordered tests? This can be accomplished by accessing the implied MockRepository via the GetMockRepository method. Here’s a … Continue reading “Ordered Tests with Rhino Mocks AAA Syntax”

Unit Testing Stored Procedure Calls with Rhino Mocks

Database stored procedure calls are one of the trickiest things to unit test, and there are many different approaches that can be taken. My team has run the gamut: test DBs that rollback with each run, no testing for direct data access functions (!), virtual functions w/ partial mocks (see here). The latest approach that … Continue reading “Unit Testing Stored Procedure Calls with Rhino Mocks”

Testing Hard-to-Test Code with Rhino Mocks

One of the most common gripes that I hear about getting started with unit testing is that the code isn’t testable or it doesn’t have a testable design. People will make claims like, “We need X hours to make the project testable before we can start writing tests.” Well, I don’t buy into that. There … Continue reading “Testing Hard-to-Test Code with Rhino Mocks”