在 Assert.Multiple 中验证模拟
Verify mocks in Assert.Multiple
是否可以验证 Assert.Multiple
块中的方法调用以及对 Assert
的其他调用?
当对 MyMethod
的方法调用未验证时,我当前的解决方案不调用 SomeProperty
上的断言。
接近我想要的唯一方法是将对 myInterfaceMock.Verify
的调用移动到最后,但是当有多个方法调用需要验证时,这不再有效。
var mInterfaceMock = new Mock<IMyInterface>()
.Setup(x => x.MyMethod())
.Verifiable();
var systemUnderTest = new MyClass(myInterfaceMock.Object);
systemUnderTest.MethodToTest();
Assert.Multiple(() => {
myInterfaceMock.Verify();
Assert.That(systemUnderTest.SomeProperty, Is.True);
});
验证将抛出断言块不知道如何处理的异常。这就是为什么当验证失败时不会在它之后调用任何东西。
文档中的注释还指出
The test will be terminated immediately if any exception is thrown that is not handled. An unexpected exception is often an indication that the test itself is in error, so it must be terminated. If the exception occurs after one or more assertion failures have been recorded, those failures will be reported along with the terminating exception itself.
考虑以下方法
//...
Assert.Multiple(() => {
Assert.That(() => myInterfaceMock.Verify(), Throws.Nothing);
Assert.That(systemUnderTest.SomeProperty, Is.True);
});
如果验证抛出异常,则将由其自己的断言处理。
如果你的 MethodToTest()
没有 return 任何东西,我建议你只验证方法 运行,就像你试图用断言做的那样,但我' d 建议这样做:
mInterfaceMock.Verify(x => x.MethodToTest(), Times.Once);
只是指定它必须 运行 的次数,而不是在断言中
是否可以验证 Assert.Multiple
块中的方法调用以及对 Assert
的其他调用?
当对 MyMethod
的方法调用未验证时,我当前的解决方案不调用 SomeProperty
上的断言。
接近我想要的唯一方法是将对 myInterfaceMock.Verify
的调用移动到最后,但是当有多个方法调用需要验证时,这不再有效。
var mInterfaceMock = new Mock<IMyInterface>()
.Setup(x => x.MyMethod())
.Verifiable();
var systemUnderTest = new MyClass(myInterfaceMock.Object);
systemUnderTest.MethodToTest();
Assert.Multiple(() => {
myInterfaceMock.Verify();
Assert.That(systemUnderTest.SomeProperty, Is.True);
});
验证将抛出断言块不知道如何处理的异常。这就是为什么当验证失败时不会在它之后调用任何东西。
文档中的注释还指出
The test will be terminated immediately if any exception is thrown that is not handled. An unexpected exception is often an indication that the test itself is in error, so it must be terminated. If the exception occurs after one or more assertion failures have been recorded, those failures will be reported along with the terminating exception itself.
考虑以下方法
//...
Assert.Multiple(() => {
Assert.That(() => myInterfaceMock.Verify(), Throws.Nothing);
Assert.That(systemUnderTest.SomeProperty, Is.True);
});
如果验证抛出异常,则将由其自己的断言处理。
如果你的 MethodToTest()
没有 return 任何东西,我建议你只验证方法 运行,就像你试图用断言做的那样,但我' d 建议这样做:
mInterfaceMock.Verify(x => x.MethodToTest(), Times.Once);
只是指定它必须 运行 的次数,而不是在断言中