我如何模拟使用NUnit的属性?
注意:我发现this外围嘲讽的回答是非常有用的,它改变用途作为一个独特的问题,在这里回答条目他人找到。
其他答案欢迎了。
NUnit的-讨论注: NUnit的嘲弄是在一个周末创建 作为玩具模拟实现[...]我开始认为这是一个错误,因为你是远 从第一人成为依赖它。的 - http://groups.google.com/group/nunit-discuss/msg / 55f5e59094e536dc (查理池在NUnit的嘲弄)
解决方案要嘲笑的姓名的下面的示例属性...
接口IVIEW {
名单<字符串>名称{获得;组;}
}
公共类presenter {
公开名单<字符串> GetNames(IVIEW视图){
返回view.Names;
}
}
NUnit的模拟解决方案的房产
使用NUnit.Mocks;
在NUnit的一个的属性名的可以用轻慢的 get_PropertyName 以嘲弄get访问和 set_PropertyName 以嘲笑set访问,使用模拟图书馆期待*(..)方法,像这样:
列表名称=新的列表{测试,测试1};
DynamicMock mockView =新DynamicMock(typeof运算(IVIEW));
mockView.ExpectAndReturn(get_Names,名称);
IVIEW视图=(IVIEW)mockView.MockInstance;
Assert.AreEqual(姓名,presenter.GetNames(视图));
因此,在我国特定的$ C顶部$ C样品中, .Names 的属性被嘲笑为任何 get_Names 或 set_Names 。
等
This博客文章提供额外的观点考虑NUnit的似乎提供了模拟的方法,唯一的目标的方法:
我就开始想这个问题, 意识到物业getter和 setter方法只是当作speciallly 在幕后的命名方法
How do I mock a property using NUnit?
NOTE: I found this peripheral mocking answer to be extremely useful and repurposed it as a distinct question and answer entry here for others to find.
Other answers welcome too.
NUnit-Discuss Note: NUnit Mocks was created over a weekend as a toy mock implementation [...] I'm beginning to think that was a mistake, because you are far from the first person to become reliant on it. -- http://groups.google.com/group/nunit-discuss/msg/55f5e59094e536dc (Charlie Pool on NUnit Mocks)
解决方案
To mock the Names property in the following example...
Interface IView {
List<string> Names {get; set;}
}
public class Presenter {
public List<string> GetNames(IView view) {
return view.Names;
}
}
NUnit Mock Solution for a Property
using NUnit.Mocks;
In NUnit a PropertyName can be mocked with get_PropertyName to mock the get accessor and set_PropertyName to mock the set accessor, using the mock library's Expect*(..) methods like so:
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));
Therefore, in our specific code sample at the top, the .Names property is mocked as either get_Names or set_Names.
Etc.
This blog post provides additional insight considering NUnit seemingly provides mock methods to only target methods:
I started thinking about it and realized that Property getters and setters are just treated as speciallly named methods under the covers
相关推荐
最新文章