开发者

Check the properties of an object passed back to mock using Moq

Is it possible to check what the properties are of an object send to a Mocked method? For example I have:

public class Foo
{
    int SomeNumber {get; set;}
}

public class ReceivesFoo: IReceivable
{
    public void Process(Foo foo)
    {
    }
}

public class Bar
{
    private IReceivable receiver;

    public void SomeMethod(int b)
    {
        Foo foo = new Foo();
        if (b == 0)
        {
            foo.SomeNumber = 12;
        }
        else
        {
            foo.SomeNumber = 7;
        }
        receiver.Process(foo);
    }
}

public class TestBar
{
    public void ZeroReceives12()
    {
        mockReceivable.Setup(x => x.Process(It.IsAny<Foo>());
        bar.SomeMethod(0);
    }
}

It is quite a simplification but hopefully you know what I am getting at. I know it is a bad idea to create a new class in a method. So tha开发者_如何转开发t is done somewhere else in a factory of sorts. But all the logic to set the various properties of the new object are done in this method. Because it makes no sense to put that logic in the factory because it has nothing to do with the factory and things will changes depending on the state of Bar.

So basically I just want to be able to do more than just verify that mocked receiver would have received some kind of Foo. I want to make sure that it received an instance of Foo and that its SomeNumber was set to 12.


This is a great reference for you... https://github.com/Moq/moq4/wiki/Quickstart

Basically you want something like this...

public void ZeroReceives12()
{
    int input;

    mockReceivable.Setup(x => x.Process(It.IsAny<Foo>())
              .Callback(y => foo = y);

    bar.SomeMethod(0);

    // ensure input is what you expect
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜