views:

92

answers:

1

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!

+1  A: 

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();
}
NerdFury
Well that was rather dumb of me, the reason I mock LogOn is because I don't want the SetUp to operate as normal. Using dependancy injection for a data provider (which was originally omitted) . I can't not mock the LogOn class (or so I think)
Slace
i fixed the DI so that I didn't need to mock the LogOn class
Slace
I assume that you have this fixed, but it appears that you are attempting to resolve the DataProvider from the DI container in the Setup method. You should instead try to have your DI container resolve the LogOn class and have the view and data provider passed in via the constructor. Otherwise, you could use Property Injection and simply set the View and DataProvider properties from outside the LogOn class. That would be easier to mock.Good luck with your project.
NerdFury