c# – 如何模拟返回列表对象的属性 – 在rhino mock中
作者:互联网
Interface IView
{
List<string> Names {get; set;}
}
public class Presenter
{
public List<string> GetNames(IView view)
{
return view.Names;
}
}
var mockView = MockRepository.GenerateMock<IView>();
var presenter = new Presenter();
var names = new List<string> {"Test", "Test1"};
mockView.Expect(v => v.Names).Return(names);
Assert.AreEqual(names, presenter.GetNames(mockView)) // Here presenter returns null which is incorrect behaviour in my case;
当我使用上面的代码返回名称的模拟列表时,它与expecatation不匹配然后返回null并失败
谢谢你的帮助
编辑:
我将视图作为演示者的GetNames方法的参数传递.这里的问题是当我从mocked属性返回list对象时它返回null.但是,当我将属性数据类型更改为string / int iepremitive类型时,则正确返回值
解决方法:
我没有看到你的mockView附加到你的演示者的任何地方.因此,从演示者的角度来看,视图为空.您可能需要执行以下操作:
presenter.View = view;
我刚用NUnit和RhinoMocks 3.5编写了这个,以确保它有效.这是我的两个类文件.测试通过了.
using System.Collections.Generic;
namespace Tests
{
public interface IView
{
List<string> Names { get; set; }
}
public class Presenter
{
public List<string> GetNames(IView view)
{
return view.Names;
}
}
}
using System.Collections.Generic;
using NUnit.Framework;
using Rhino.Mocks;
namespace Tests
{
[TestFixture]
public class TestFixture
{
[Test]
public void TestForStackOverflow()
{
var mockView = MockRepository.GenerateMock<IView>();
var presenter = new Presenter();
var names = new List<string> {"Test", "Test1"};
mockView.Expect(v => v.Names).Return(names);
Assert.AreEqual(names, presenter.GetNames(mockView));
}
}
}
我只能猜测你混淆代码的方式有问题.
标签:c,mocking,rhino-mocks 来源: https://codeday.me/bug/20190627/1302336.html