如何使用NUnit模拟属性?
注意:我发现this peripheral mocking answer非常有用,并将其作为一个独特的问答条目重新用于其他人,以供其他人查找。
其他答案也欢迎。
NUnit-Discuss注意: NUnit Mocks是在周末创建的 作为玩具模拟实现[...]我开始认为这是一个错误,因为你很远 从第一个人变得依赖它。
- http://groups.google.com/group/nunit-discuss/msg/55f5e59094e536dc
(NUnit Mocks上的Charlie Pool)
答案 0 :(得分:9)
在以下示例中模拟名称属性...
Interface IView {
List<string> Names {get; set;}
}
public class Presenter {
public List<string> GetNames(IView view) {
return view.Names;
}
}
using NUnit.Mocks;
在NUnit中,可以使用 get_PropertyName 模拟 get_PropertyName 来模拟get访问器,使用 set_PropertyName 模拟设置访问器,使用模拟库期待*(..)这样的方法:
List names = new List {"Test", "Test1"};
DynamicMock mockView = new DynamicMock(typeof(IView));
mockView.ExpectAndReturn("get_Names", names);
IView view = (IView)mockView.MockInstance;
Assert.AreEqual(names, presenter.GetNames(view));
因此,在顶部的特定代码示例中, .Names 属性被模拟为 get_Names 或 set_Names 。
This blog post提供了额外的见解,考虑到NUnit似乎只为目标方法提供了模拟方法:
我开始考虑它了 意识到物业的吸气者和 塞特犬只是特别对待 封面下的命名方法