Rhino Mock newbie issues

So I am new to rhino nostalgia and I am trying to run it on an MVP project that I am working on. So, I have an interface representing my view and class for my presenter, for example:

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 method DoStuff

, so I have 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 reference exception from the method DoStuff

(during wait setting) because the View object is null. And here I am stuck. I called a method Init

that assigns a value to a property View

and I thought the wait point is that the method itself was never called?

+1


source to share


4 answers


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

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

      

You may also need to use the new AAA syntax (Arrange-Act-Assert) 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 agree with @ayende that you should probably only layout / stub the view, not the presenter. I'll keep the original amendment to get your code to work, but update your example to reflect the latter.

+2


source


It looks like you are testing Rhino Mocks. You mock the presentation and the presenter. I'm going to suggest that mocking the presenter is not desirable and you should only mock the presentation.



+8


source


From what I can see in your source code and your tests, it is difficult to understand what you are actually trying to test. View? Leading?

Since you have an interface for your view and a class implementation for your presenter, I think you want to mock the view and test the presenter.

Here is the group site (I sent you a link to this question):

http://groups.google.com/group/RhinoMocks

See this code. This is from 2007, but it might make you laugh.

http://tech.groups.yahoo.com/group/AgileEmbedded/files/HomeGuard/

0


source


As others have written, you need to decide what code you are trying to test and only mock the other. If you are trying to test a presenter you only want to mock / close the window while still using the real presenter. Something like that:

[TestMethod]
public void Test(){
  var view = MockRepository.GenerateStub<IView>();
  var presenter = new Presenter();
  presenter.Init(view);
  view.SomeData = "Test";
  Assert.AreEqual(presenter.DoStuff(), "Done stuff with Test");
}

      

Then you check that the Presenter.DoStuff () method is using the view correctly.

Hope it helps.

0


source







All Articles