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();
}
Am Montag, 27. Februar 2012 22:45:15 UTC+1 schrieb Ryeknow:
>
> 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.
--
You received this message because you are subscribed to the Google Groups
"Rhino.Mocks" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/rhinomocks/-/IXoWqc90JosJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rhinomocks?hl=en.