使用最小起订量验证具有不同参数的单独调用
我试图验证传递给后续模拟方法调用(同一方法)的参数值,但无法找出有效的方法。一个通用示例如下:
public class Foo
{
[Dependency]
public Bar SomeBar
{
get;
set;
}
public void SomeMethod()
{
this.SomeBar.SomeOtherMethod("baz");
this.SomeBar.SomeOtherMethod("bag");
}
}
public class Bar
{
public void SomeOtherMethod(string input)
{
}
}
public class MoqTest
{
[TestMethod]
public void RunTest()
{
Mock<Bar> mock = new Mock<Bar>();
Foo f = new Foo();
mock.Setup(m => m.SomeOtherMethod(It.Is<string>("baz")));
mock.Setup(m => m.SomeOtherMethod(It.Is<string>("bag"))); // this of course overrides the first call
f.SomeMethod();
mock.VerifyAll();
}
}
在设置中使用函数可能是一个选项,但似乎我会被简化为某种全局变量来知道我正在验证哪个参数/迭代。也许我忽略了起订量框架中显而易见的内容?
I'm trying to validate the values of arguments passed to subsequent mocked method invocations (of the same method), but cannot figure out a valid approach. A generic example follows:
public class Foo
{
[Dependency]
public Bar SomeBar
{
get;
set;
}
public void SomeMethod()
{
this.SomeBar.SomeOtherMethod("baz");
this.SomeBar.SomeOtherMethod("bag");
}
}
public class Bar
{
public void SomeOtherMethod(string input)
{
}
}
public class MoqTest
{
[TestMethod]
public void RunTest()
{
Mock<Bar> mock = new Mock<Bar>();
Foo f = new Foo();
mock.Setup(m => m.SomeOtherMethod(It.Is<string>("baz")));
mock.Setup(m => m.SomeOtherMethod(It.Is<string>("bag"))); // this of course overrides the first call
f.SomeMethod();
mock.VerifyAll();
}
}
Using a Function in the Setup might be an option, but then it seems I'd be reduced to some sort of global variable to know which argument/iteration I'm verifying. Maybe I'm overlooking the obvious within the Moq framework?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
并不是我完全错了或者白蚁太宽容了,
但更好的答案由以下代码演示:
这表明设置与验证完全分开。在某些情况下,这意味着参数匹配必须进行两次:(
Not that I was completely wrong or Termite too tolerant,
but the better answer is demonstrated by the following code:
This shows that the setup is completely separated from validation. In some cases it means that argument matching has to be done twice :(