R – Beginner Rhino Mock problem

netrhino-mocks

So I'm new to rhino mocks and I'm trying to get it going in a MVP patterned project I'm on. So I've got an interface representing my View and a class for my Presenter like so:

public interface IView {
  string SomeData { get; set; }
}

public class Presenter {
  public IView View { get; set; }
  public void Init(IView view) {
    this.View = view;
  }

  public virtual string DoStuff(){
    return "Done stuff with " + this.View.SomeData;
  }
}

And I'm trying to set up a test to mock the DoStuff method, so I've got a basic fixture like this:

[TestMethod]
public void Test(){
  var mocks = new MockRepository();
  var view = mocks.Stub<IView>();
  var presenter = mocks.StrictMock<Presenter>();

  presenter.Init(view);

  using(mocks.Record()){
    presenter.Expect(p => p.DoStuff()).Return("Mocked result");
  }

  string result = string.Empty;
  using(mocks.Playback()){
    result = presenter.DoStuff();
  }

  Assert.AreEqual(result, "Mocked result");
}

But I keep getting a null reference exception from within the DoStuff method (during the expectation setup), because the View object is null. And this is where I'm stuck. I've called the Init method, which assigns the value of the View property, and I thought that the point of an expectation setup was that the method itself wasn't ever called?

Best Answer

You need to mock the View property as well, instead of calling the Init method on the mocked presenter.

presenter.Expect( p => p.View ).Return( view );

You might also want to look at using the new AAA (Arrange-Act-Assert) syntax for RhinoMocks.

string expectedResult = "Done stuff with Mocked Result";

var view = MockRepository.GenerateMock<IView>();
view.Expect( v => v.SomeData ).Return( "Mocked Result" );

var presenter = new Presenter();
presenter.Init( view );

string actualResult = presenter.DoStuff();

Assert.AreEqual( expectedResult, actualResult );

view.VerifyAllExpectations();

EDIT After looking at this again, I would agree with @ayende that you should probably only mock/stub the view not the presenter. I'll leave the original correction to get your code to work, but update my example to reflect the latter.

Related Topic