Question

I'm setting up some RhinoMock tests but I can't work out why my expectations are failing.

Here are the class/ interface I'm testing:

public class LogOn {
    public virtual ILogOn View { get; set; }
    public virtual IDataProvider DataProvider { get; set; }

    public void SetUp(ILogOn view) {
        this.View = view;
        this.DataProvider = ... //using dependancy injection to do the data provider, so I want it faked in tests
    }
    public void SetUpEvents() {

        this.View.Submit += new EventHandler(View_Submit);
    }

    void View_Submit(object sender, EventArgs e) {
        if ( this.DataProvider.LogOn(this.Username) ) {
            this.View.SubmitSuccess();
        } else {
            this.View.SubmitFailure("Username is incorrect");
        }
    }
}

public interface ILogOn {
    string Username { get; set; }
    event EventHandler Submit;
    void SubmitSuccess();
    void SubmitFailure(string message);
}

And here is my test method:

[TestMethod]
public void LogOnFailure() {
    var dataProvider = MockRepository.CreateStub<DataProvider>();
    var presenter = MockRepository.CreateMock<LogOn>();
    var view = MockRepository.CreateMock<ILogOn>();

    dataProvider.Expect(d => d.LogOn(null)).Return(true).Repeat.Any();

    presenter.Expect(p => p.DataProvider).Return(dataProvider).Repeat.Any();
    presenter.Expect(p => p.View).Return(view).Repeat.Any();
    presenter.Expect(p => p.SetUpEvents()).CallOriginalMethod();

    view.Expect(v => v.Username).Return("invalid").Repeat.Any();
    view.Expect(v => v.SubmitFail(null)).Constraints(Is.Same("Username is incorrect"));

    presenter.SetUp(view);
    presenter.SetUpEvents();

    view.Raise(v => v.Submit += null, null, EventArgs.Empty);

    presenter.VerifyAllExpectations();
    view.VerifyAllExpectations();
}

The expectation that is failing is:

view.Expect(v => v.SubmitFail(null)).Constraints(Is.Same("Username is incorrect"));

(indicated by view.VerifyAllExpectations)

It says that that method is never executed, but when using the debugger I can step through and LogOn.View is accessed, does call the SubmitFailure method (with that argument) and return correctly.

I can't work out what is missing as watching the code does indicate that everything is executed at the right time and with the right values.

Edit: Ok, so I let out the code which is why I was mocking the LogOn class, it has a dependancy of an external data provider (which I'm stubbing as I don't care how it works). My appologies, I thought I was making this clearer but just made is worse!

Was it helpful?

Solution

The LogOn class is your system under test, so you should not be mocking that. You want to test that the LogOn class behaves as it should in the case of an invalid username. You are able to determine the correct behavior by passing in a mocked view that sets up the scenario you want. Try changing your test to what I have below.

[TestMethod]
public void LogonFailure()
{
    var presenter = new LogOn();
    var view = MockRepository.CreateMock<ILogOn>();

    view.Expect(v => v.Username).Return("invalid").Repeat.Any();
    view.Expect(v => v.SubmitFail(null)).Constraints(Is.Same("Username is incorrect"));

    presenter.Setup(view);

    view.Raise(v => v.Submit += null, null, EventArgs.Empty);

    view.VerifyAllExpectations();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top