fais
fais

Reputation: 175

Testing with Rhino Mock throws an error

Hello I am kind of a beginner level with TDD and using RhinoMocks for creating moqs in application. I am trying to implement MVP pattern.

Here is my interface

public interface IView
{
   List<Bundle> DisplayList { get; set; }  
}

and my Presenter class

public class Presenter
{
    private IView View;
    public Presenter(IView view)
    {
        View = view;            
    }

    public void Bind()
    { 
        // I am creating a dummy list in MockDataLayer and SelectAll Method returns the whole list
        IDataLayer dsl=new MockDataLayer();
        View.DisplayList = dsl.SelectAll();
    } 
}

Below is my test class

public class PresenterTest 
{

    private IView _view;
    private Presenter _controller;

    [Test]
    public void View_Display_List()
    {
        //Arrange
        _view = MockRepository.GenerateMock<IView>();
        List<Bundle> objTest = new List<Bundle>();            
        _controller = new Presenter(_view);
        _view.Expect(v => v.DisplayList).Return(objTest);

        //Act
        _controller.Bind();

        //Assert
        _view.VerifyAllExpectations();
    }
}

When I execute my test, I recieve this error:

depaulOAR.PatchBundleTesting.Test.BundlePresenterTest.BundleView_Display_Bundle_List:
Rhino.Mocks.Exceptions.ExpectationViolationException : IBundleView.get_DisplayList(); Expected #1, Actual #0.

Any help will be highly appreciated.

EDIT: NOTE I am getting help from this link. Almost everything is working except the test part. When I implement it on Web form, my browser displays the list. But when I test the View it throws an error http://www.bradoncode.com/blog/2012/04/mvp-design-pattern-survival-kit.html

Thanks "Old Fox" for your help. But now my issue is it's throwing a different error

Upvotes: 0

Views: 333

Answers (1)

Old Fox
Old Fox

Reputation: 8725

You initialize an expectation on the IView.DisplayList's getter:

_view.Expect(v => v.DisplayList).Return(objTest);

The above line put's an expectation on the getter.

In the method under test you use the IView.DisplayList's setter:

View.DisplayList = dsl.SelectAll();

I believes that the behavior you want to test is: "The items to display was set in the view". If so, your test should be something similar to:

[Test]
public void View_Display_List()
{
   //Arrange
   _view = MockRepository.GenerateMock<IView>();
   List<Bundle> objTest = new List<Bundle>();
   controller = new Presenter(_view);

   //Act
   _controller.Bind();

   //Assert
   CollectionAssert.AreEquivalent(The same items MockDataLayerl.SelectAll() returns 
                                  ,_view.DisplayList );
}

Edit:

Verifies that something was assign to View.DisplayList is easier then the above example. You have to verify that View.DisplayList is not null:

[Test]
public void View_Display_List()
{
   //Arrange
   _view = MockRepository.GenerateMock<IView>();
   _view.Stub(x => x.Display List).Property Behavior();
   List<Bundle> objTest = new List<Bundle>();
   controller = new Presenter(_view);

   //Act
   _controller.Bind();

   //Assert
   Assert.IsNotNull(_view.DisplayList );
} 

Upvotes: 1

Related Questions