开发者

How do I test "Parameters Passed In Final Call" using RhinoMocks?

What's the best way in Rhino Mocks to test that a particular parameter is passed on the FINAL call to a method? Eg mockview.SetSomething(myObj) might be called any number of times by mockview.Init, but I want to test that the last time it's called as mockview.SetSomething(inPart开发者_如何学JAVAicular).

Now I know I can use GetArgumentsForCallsMadeOn with this, but my problem is that it doesn't work if I've subsequently changed the parameter holding variable. e.g

public interface IView
{
    void SetSomething(ViewData data);
}

public class ViewData
{
    public int Age { get; set; }
    public string Name { get; set; }

    public ViewData Person(int age, string name)
    {
        Age = age;
        Name = name;
        return (this);
    }
}

public class WorkingPresenter
{
    public void Init(IView view)
    {
        var data = new ViewData {Age = 1, Name = "One"};
        view.SetSomething(data);
        data = new ViewData {Age = 2, Name = "Two"};
        view.SetSomething(data);
        data = new ViewData {Age = 3, Name = "Three"};
    }
}

public class NotWorkingPresenter
{
    private ViewData _data;

    public void Init(IView view)
    {
        _data = new ViewData();
        view.SetSomething(_data.Person(1, "One"));
        view.SetSomething(_data.Person(2, "Two"));
        _data.Person(3, "Three");

    }
}

then my tests are ...

    [Test]
    public void GetDataOfLastCall()
    {
        ViewData dummydata=null;
        var view = MockRepository.GenerateStub<IView>();

        //Approach 1 : This works 
        var workingPresenter = new WorkingPresenter();
        workingPresenter.Init(view);
        var lastCall = view.GetArgumentsForCallsMadeOn(v => v.SetSomething(dummydata)).Count - 1;
        var lastParams = view.GetArgumentsForCallsMadeOn(v => v.SetSomething(dummydata))[lastCall];
        var lastData = (ViewData)lastParams[0];

        //Approach 2: This doesn't
        var notWorkingPresenter = new NotWorkingPresenter();
        notWorkingPresenter.Init(view);
        lastCall = view.GetArgumentsForCallsMadeOn(v => v.SetSomething(dummydata)).Count - 1;
        lastParams = view.GetArgumentsForCallsMadeOn(v => v.SetSomething(dummydata))[lastCall];
        lastData = (ViewData)lastParams[0];

What I want is to verify that the last call to SetSomething was with {name="Two", age=2}. Now workingPresenter does this but wouldn't you expect notWorkingPresenter to do so too?


There must be something else going on in your code (outside of the mocking). I just threw together a few items:

public interface IView
{
    void SetSomething(ViewData data);
}

public class ViewData
{
    public int Age { get; set; }
    public string Name { get; set; }
}

And I tested it with:

[TestMethod]
public void GetDataOfLastCall()
{
    var view = MockRepository.GenerateStub<IView>();

    var data = new ViewData {Age = 1, Name = "One"};
    view.SetSomething(data);
    data = new ViewData { Age = 2, Name = "Two" };
    view.SetSomething(data);
    data = new ViewData { Age = 3, Name = "Three" };

    var lastCall = view.GetArgumentsForCallsMadeOn(v => v.SetSomething(data)).Count - 1;
    var lastParams = view.GetArgumentsForCallsMadeOn(v => v.SetSomething(data))[lastCall];
    var lastData = (ViewData) lastParams[0];
}

And I got the values of 2 and "Two" inside the ViewData. It appears Rhino.Mocks supports what you want to do. Could you create a failing test case that shows the issue identified in your original question (where you got a reference to the most recent information)?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜