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

馋奶兔 提交于 2019-12-12 09:45:59

问题


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

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. However when i change the property data type to string/int i.e.premitive type then value is returned correctly


回答1:


I don't see anywhere where your mockView is getting attached to your presenter. 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. 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.




回答2:


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




回答3:


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:

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));




回答4:


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.



来源:https://stackoverflow.com/questions/591174/how-to-mock-the-property-which-returns-the-list-object-in-rhino-mock

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!