Nunit DynamicMock C# Presenter 类

发布于 2024-10-30 13:19:54 字数 730 浏览 1 评论 0原文

我是 Nunit 测试的新手,我希望有人能为我提供一个简短的解释,甚至是一个网站链接,我可以在其中获得使用 DynamicMock.ExpectAndReturn 的很好的解释和示例代码。

我正在尝试 MVP 模式,我的演示者类已设置单元测试,如下所示

mock = new DynamicMock(typeof(I_MyInterface));
View = new MyPresenterClass((I_MyInterface)mock.MockInstance);
view.Initialise();

我有一个字符串属性“名称”,我想确保下面的代码是有效的演示者测试?我尝试了以下代码

mock.ExpectAndReturn("get_Name", "Yoda");
Assert.AreEqual("Yoda", ((I_MyInterface)mock.MockInstance).Name);

我的最后一个问题是,如何测试演示者正确返回值并正确设置属性的方法。例如,

public bool NameIt(int i)
{
     if(i<20)
     {
          view.Name="Yoda";
          return true;
     }
     return false;
 }

有人可以告诉我如何通过动态模拟来测试这个方法吗?

先感谢您!

im new to Nunit testing and I was hoping someone could please provide me with a brief explaination or even a link to a site where I can get a nice explaination and example code for using DynamicMock.ExpectAndReturn.

Im trying out the MVP pattern, and my presenter class I have setup unit test as follows

mock = new DynamicMock(typeof(I_MyInterface));
View = new MyPresenterClass((I_MyInterface)mock.MockInstance);
view.Initialise();

I have a string property "Name", I wanted to make sure that the code below is a valid presenter test? I tried the following code

mock.ExpectAndReturn("get_Name", "Yoda");
Assert.AreEqual("Yoda", ((I_MyInterface)mock.MockInstance).Name);

My last question is, how can I test a method form my presenter correctly returned a value and set a property correctly. For example a method

public bool NameIt(int i)
{
     if(i<20)
     {
          view.Name="Yoda";
          return true;
     }
     return false;
 }

Could someone please show me how I could test this via the dynamic mocking?

Thank you in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自由范儿 2024-11-06 13:19:54

您需要使用 ExpectAndReutrn。

 // Tell that mock object when the "GetPeople" method is 
 // called to return a predefined list of people
 personRepositoryMock.ExpectAndReturn("GetPeople", peopleList);

完整的示例是 http://www.zorched.net /2007/03/10/mocking-net-objects-with-nunit/

You need to use ExpectAndReutrn.

 // Tell that mock object when the "GetPeople" method is 
 // called to return a predefined list of people
 personRepositoryMock.ExpectAndReturn("GetPeople", peopleList);

Full example is http://www.zorched.net/2007/03/10/mocking-net-objects-with-nunit/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文