使用 Moq 检查传回模拟的对象的属性
是否可以检查发送到模拟方法的对象的属性是什么?例如我有:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这对您来说是一个很好的参考... https://github.com/Moq/moq4/wiki /快速入门
基本上你想要这样的东西......
This is a great reference for you... https://github.com/Moq/moq4/wiki/Quickstart
Basically you want something like this...