Difference between new MockRespository().Stub<> vs MocksRepository.GenerateStub<>() ?

707 views
Skip to first unread message

Ryeknow

unread,
Feb 27, 2012, 4:45:15 PM2/27/12
to Rhino.Mocks
I was introduced to Rhino Mocks via the book the Art of Unit Testing
and just starting using it a few days ago. From the examples I read,
all of the unit tests used the Record & Replay model, the author did
mentioned that support for the Arrange, Act, and Assert model
exists.

I've been scouring the internet, reading documentation, etc to try to
find out how to go about doing this to test if a method is returning
an expected value. Below is my code:

public interface IPilotManager {
List<Pilot> Search(Pilot p);
int Save(Pilot p);
}

public class Pilot {
public int PilotID { get; set; }
public string FirstName { get; set; }
public string MiddleInitial { get; set; }
public string LastName { get; set; }
}

And below is my unit test:

[TestMethod]
public void PilotSearch_SearchCriteriaSpecified_PilotFound() {
//This logic fails because the call to the Search method in the Assert
line doesn't return the List of Pilots
IPilotManager pilotMngrStub = Mocks.Stub<IPilotManager>();
pilotMngrStub.Expect(p => p.Search(null)).Return(new List<Pilot>()
{ TestPilot });
Assert.IsTrue(pilotMngrStub.Search(null).Contains(TestPilot),
"Failed");

//This logic passes. The call to Search returns a List of pilots in
the Search method.
IPilotManager pilotMngrStub =
MockRepository.GenerateStub<IPilotManager>();
pilotMngrStub.Expect(p => p.Search(null)).Return(new List<Pilot>()
{ TestPilot });
Assert.IsTrue(pilotMngrStub.Search(null).Contains(TestPilot),
"Failed");
}

What is the difference between the two? I thought the
MockRepository.GenerateStub was introduced as a way to reduce book
keeping (as the documentation states), but it looks like there is a
difference that I'm not picking up.

sven

unread,
Mar 1, 2012, 10:47:38 AM3/1/12
to rhino...@googlegroups.com
You will only notice a difference between MockRepository.GenerateStub and MockRepository.GenerateMock when you call VerifyAllExpectations(). The only purpose of stubs is to feed indirect input into the system under test. Stubs will never throw exceptions to fail the test. Mocks can also feed indirect input into the system under test, but they check that their own methods where called and throw exceptions otherwise. Here are two additional tests illustrating the difference:

        [Test]
        public void PilotSearch_SearchCriteriaSpecified_SearchNotCalled_WithStub()
        {
            // Setup

            IPilotManager pilotMngrStub = MockRepository.GenerateStub<IPilotManager>();
            pilotMngrStub.Expect(p => p.Search(null)).Return(new List<Pilot>() { testPilot });

            // Execute -> We do not call the Search() method, although it is expected

            // Verify -> stubs will never throw exceptions
            pilotMngrStub.VerifyAllExpectations();
        }

        [Test]
        [ExpectedException(typeof(ExpectationViolationException))]
        public void PilotSearch_SearchCriteriaSpecified_SearchNotCalled_WithMock()
        {
            // Setup
            IPilotManager pilotMngrMock = MockRepository.GenerateMock<IPilotManager>();
            pilotMngrMock.Expect(p => p.Search(null)).Return(new List<Pilot>() { testPilot });

            // Execute -> We do not call the Search() method, although it is expected

            // Verify- > will throw ExpectationViolationException
            pilotMngrMock.VerifyAllExpectations();

Griff

unread,
Jun 13, 2012, 4:27:38 AM6/13/12
to rhino...@googlegroups.com


On Thursday, March 1, 2012 3:47:38 PM UTC, sven wrote:
You will only notice a difference between MockRepository.GenerateStub and MockRepository.GenerateMock when you call VerifyAllExpectations().

Sven, I think the question was really: what is the difference between creating a STUB from the static MockRepository class vs creating a STUB from an instance of the MockRepository [new Rhino.Mocks.MockRepository()].

Personally, I use the static version but that's only for the following reasons:
  • Why create an instance when I can use the static?
  • The documentation I was using for the instance way just seemed difficult to follow
  • It works for my particular test scenarios
However, what the actual differences are (if there are any) I can't really say.

Griff

bill richards

unread,
Jun 13, 2012, 1:35:58 PM6/13/12
to rhino...@googlegroups.com
The difference between the two calls
 
new MockRepository().GenerateStub<ISomeType>()
Mocks.GenerateStub<ISomeType>();
 
is about the methodology you are using for your tests, i.e. are you using Play/Record syntax (the former), or are you using AAA syntax (the latter).
Reply all
Reply to author
Forward
0 new messages