如何使用NUnit嘲笑的属性? [英] How to mock a property using NUnit?

查看:198
本文介绍了如何使用NUnit嘲笑的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何模拟使用NUnit的属性?


注意:我发现<一href="http://stackoverflow.com/questions/591174/how-to-mock-the-property-which-returns-the-list-object-in-rhino-mock/594933#594933">this外围嘲讽的回答是非常有用的,它改变用途作为一个独特的问题,在这里回答条目他人找到。

其他答案欢迎了。

  

NUnit的-讨论注:    NUnit的嘲弄是在一个周末创建   作为玩具模拟实现[...]我开始认为这是一个错误,因为你是远   从第一人成为依赖它。
   - http://groups.google.com/group/nunit-discuss/msg / 55f5e59094e536dc
  (查理池在NUnit的嘲弄)

解决方案

要嘲笑的姓名的下面的示例属性...

 接口IVIEW {
    名单&LT;字符串&GT;名称{获得;组;}
}

公共类presenter {
    公开名单&LT;字符串&GT; 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


<一个href="http://www.vergentsoftware.com/blogs/ckinsman/usingNUnitMocksDynamicMockWithAPropertyInC.aspx">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

这篇关于如何使用NUnit嘲笑的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆