如何嘲笑它返回列表对象属性 - 在犀牛模拟 [英] How to mock the property which returns the list object - In rhino mock

查看:431
本文介绍了如何嘲笑它返回列表对象属性 - 在犀牛模拟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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和失败

When I use the above code to return the mock list of names ,it doesn't match the expecatation then returns null and fails

感谢您的帮助。

编辑:
我传递的观点作为参数设置为演示者的GetNames method.Here问题是当我从嘲笑财产返还列表对象则返回null。然而,当我更改属性的数据类型为字符串/ INT iepremitive类型,然后正确返回值

I am passing the view as the paramter to presenter's GetNames method.Here the problem is when i return list object from the mocked property it returns null. However when i change the property data type to string/int i.e.premitive type then value is returned correctly

推荐答案

感谢您的帮助,调查后,我发现我是创建具有查看列表对象的相同内容的演示里一个新的列表对象,因为这一点,有人失败。
现在我使用的属性约束匹配预期的参数和它的工作!
感谢所有

Thanks for your help, after investigating I found that I was creating a new list object inside the presenter with the same content of view list object, and because of this it was failing. Now I used the property constraints to match the parameters in expectation and it worked!! Thanks all

这篇关于如何嘲笑它返回列表对象属性 - 在犀牛模拟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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