使用 Rhino Mock 测试会抛出错误

Testing with Rhino Mock throws an error

您好,我是 TDD 的初学者,使用 RhinoMocks 在应用程序中创建最小起订量。我正在尝试实现 MVP 模式。

这是我的界面

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

和我的主持人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();
    } 
}

下面是我的测试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();
    }
}

当我执行我的测试时,我收到这个错误:

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

任何帮助将不胜感激。

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

您对 IView.DisplayList 的 getter 初始化了一个期望:

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

上面一行对 getter.

提出了期望

在被测方法中,您使用 IView.DisplayList 的 setter:

View.DisplayList = dsl.SelectAll();

我认为您要测试的行为是:"The items to display was set in the view"。 如果是这样,您的测试应该类似于:

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