Testing with Rhino Mock throws an error

时间:2015-07-28 17:12:01

标签: c# asp.net unit-testing mocking rhino-mocks

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

1 个答案:

答案 0 :(得分:1)

您初始化对IView.DisplayList的getter:

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

以上一行是对吸气剂的期望。

在测试方法中,您使用IView.DisplayList的设置器:

View.DisplayList = dsl.SelectAll();

我认为您要测试的行为是:&#34;要显示的项目已在视图中设置&#34;。 如果是这样,您的测试应该类似于:

[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 );
}

修改

验证分配给View.DisplayList的内容比上面的示例更容易。 您必须验证View.DisplayList不是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 );
}