Short
Short

Reputation: 7837

rhinomocks setting expectation, unit test always passes

I'm trying to become more familiar with the Rhinomocks framework, and I'm trying to understand the Expect methods of rhinomocks.

Here's a unit test I have written:

[TestMethod]
public void Create_ValidModelData_CreatesNewEventObjectWithGivenSlugId()
{
    //Arrange
    var eventList = new List<Event>() { new Event() { Slug = "test-user" } };

    _stubbedEventRepository.Stub(x => x.GetEvents())
        .Return(eventList);

    _stubbedEventRepository
        .Expect(x => x.SaveEvent(eventList.SingleOrDefault()))
        .Repeat
        .Once();

    var controller = new EventController(_stubbedEventRepository);
    EventViewModel model = new EventViewModel();

    //Act
    //controller.Create(model); COMMENTED OUT

    //Assert
    _stubbedEventRepository.VerifyAllExpectations();
}

I thought I understood this code to only pass if the SaveEvent(...) method get's called exactly once. However, with controller.Create(model) commented out, the test still passes. Inside controller.Create(model) is where the SaveEvent() method gets called.

I tried the following:

_stubbedEventRepository
    .Expect(x => x.SaveEvent(eventList.SingleOrDefault()));

But it still passes every time, so what am I doing incorrectly stack overflow? The sources I have looked at online haven't been able to help me. Why is VerifyAllExpectations() yielding a successful unit test? Thank you!

Here's the body of the controller constructor:

public EventController(IEventRepository eventRepository)
{
    _eventRepository = eventRepository;
}

edit:

// member variables
private IEventRepository _stubbedEventRepository;

    [TestInitialize]
    public void SetupTests()
    {
        _stubbedEventRepository = MockRepository.GenerateStub<IEventRepository>();
    }

Upvotes: 2

Views: 324

Answers (1)

GoodKhaos
GoodKhaos

Reputation: 56

If you want to verify the behavior of the code under test, you will use a mock with the appropriate expectation, and verify that. If you want just to pass a value that may need to act in a certain way, but isn't the focus of this test, you will use a stub.

Upvotes: 4

Related Questions