简体   繁体   English

如何模拟返回列表对象的属性 - 在rhino mock中

[英]How to mock the property which returns the list object - In rhino mock

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;

When I use the above code to return the mock list of names ,it doesn't match the expecatation then returns null and fails 当我使用上面的代码返回名称的模拟列表时,它与expecatation不匹配然后返回null并失败

thanks for your help 谢谢你的帮助

Edit: 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. 编辑:我将视图作为主持人的GetNames方法的参数传递。问题是,当我从mocked属性返回列表对象时,它返回null。 However when i change the property data type to string/int iepremitive type then value is returned correctly 但是,当我将属性数据类型更改为string / int iepremitive类型时,将正确返回值

I don't see anywhere where your mockView is getting attached to your presenter. 我没有看到你的mockView附加到你的演示者的任何地方。 So from the presenter's point of view, the view is null. 因此,从演示者的角度来看,视图为空。 You might have to do something like: 您可能需要执行以下操作:

presenter.View = view; 

I just coded this with NUnit and RhinoMocks 3.5 to make sure it works. 我刚用NUnit和RhinoMocks 3.5编写了这个,以确保它有效。 Here's my two class files. 这是我的两个类文件。 The test passed. 测试通过了。

using System.Collections.Generic;

namespace Tests
{
    public interface IView
    {
        List<string> Names { get; set; }
    }

    public class Presenter
    {
        public List<string> GetNames(IView view)
        {
            return view.Names;
        }
    }
}

using System.Collections.Generic;
using NUnit.Framework;
using Rhino.Mocks;

namespace Tests
{

    [TestFixture]
    public class TestFixture
    {
        [Test]
        public void TestForStackOverflow()
        {
            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));
        }
    }
}

I can only guess you are doing something wrong with the way you've mixed up your code. 我只能猜测你混淆代码的方式有问题。

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 谢谢大家

I'm not familiar with Rhino Mocks but I can tell you how to do this with NUnit's built-in mock library, NUnit.Mocks: 我不熟悉Rhino Mocks,但我可以告诉你如何使用NUnit的内置模拟库NUnit.Mocks:

List names = new List {"Test", "Test1"}; 列表名称=新列表{“测试”,“测试1”};

DynamicMock mockView = new DynamicMock(typeof(IView)); DynamicMock mockView = new DynamicMock(typeof(IView));

mockView.ExpectAndReturn("get_Names", names); mockView.ExpectAndReturn(“get_Names”,名字);

IView view = (IView)mockView.MockInstance; IView view =(IView)mockView.MockInstance;

Assert.AreEqual(names, presenter.GetNames(view)); Assert.AreEqual(names,presenter.GetNames(view));

One thing you should not forget (I know I did and it got me confused): specify how many times you want the expectation to work - otherwise if your code uses the property more than once, you will get weird results, since the expectation 有一件事你不应该忘记(我知道我做了,这让我困惑): 指定你希望工作多少次 - 否则如果你的代码不止一次使用该属性,你会得到奇怪的结果,因为期望

mockView.Expect(v => v.Names).Return(names);

works for a single call only. 仅适用于单个呼叫。 So you should write 所以你应该写

mockView.Expect(v => v.Names).Return(names).Repeat.Any();

if your mocked property is supposed to return the same stuff every time it's called. 如果你的模拟属性应该在每次被调用时返回相同的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM