【发布时间】:2011-01-10 00:48:14
【问题描述】:
我正在尝试验证传递给后续模拟方法调用(相同方法)的参数值,但无法找出有效的方法。一个通用的例子如下:
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();
}
}
在设置中使用函数可能是一种选择,但似乎我会被简化为某种全局变量来知道我正在验证哪个参数/迭代。也许我忽略了 Moq 框架中的明显内容?
【问题讨论】:
标签: c# unit-testing moq