使用 Moq 检查传回模拟的对象的属性

发布于 2024-11-01 05:53:52 字数 901 浏览 0 评论 0原文

是否可以检查发送到模拟方法的对象的属性是什么?例如我有:

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

这是一个相当简单的描述,但希望你知道我的意思。我知道在方法中创建新类是一个坏主意。所以这是在工厂的其他地方完成的。但是设置新对象的各种属性的所有逻辑都是在这个方法中完成的。因为将该逻辑放在工厂中是没有意义的,因为它与工厂无关,并且事情会根据 Bar 的状态而变化。

所以基本上我只想能够做更多的事情,而不仅仅是验证模拟的接收者是否会收到某种 Foo。我想确保它收到了 Foo 的实例并且其 SomeNumber 设置为 12。

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 that 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜无邪 2024-11-08 05:53:52

这对您来说是一个很好的参考... https://github.com/Moq/moq4/wiki /快速入门

基本上你想要这样的东西......

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
}

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文