【发布时间】:2011-05-08 23:59:31
【问题描述】:
设置如下:
public interface IFoo
{
void Fizz();
}
[Test]
public void A()
{
var foo = new Mock<IFoo>(MockBehavior.Loose);
foo.Object.Fizz();
foo.Verify(x => x.Fizz());
// stuff here
foo.Verify(x => x.Fizz(), Times.Never()); // currently this fails
}
基本上我想在// stuff here 输入一些代码以使foo.Verify(x => x.Fizz(), Times.Never()) 通过。
因为这可能构成最小起订量/单元测试滥用,我的理由是我可以这样做:
[Test]
public void Justification()
{
var foo = new Mock<IFoo>(MockBehavior.Loose);
foo.Setup(x => x.Fizz());
var objectUnderTest = new ObjectUnderTest(foo.Object);
objectUnderTest.DoStuffToPushIntoState1(); // this is various lines of code and setup
foo.Verify(x => x.Fizz());
// reset the verification here
objectUnderTest.DoStuffToPushIntoState2(); // more lines of code
foo.Verify(x => x.Fizz(), Times.Never());
}
基本上,我有一个状态对象,需要做相当多的工作(无论是在制作各种模拟对象还是其他方面)将其推入 State1。然后我想测试从 State1 到 State2 的转换。我宁愿重复使用 State1 测试,而不是复制或抽象代码,而是将其推送到 State2 并执行我的 Asserts - 除了验证调用之外,我可以做所有这些。
【问题讨论】:
标签: c# unit-testing moq