如何验证一个方法调用一次带起订量?
问题描述:
我如何验证方法调用一次带起订量?验证()与Verifable()的东西实在是令人困惑的。
How do I verify a method was called exactly once with Moq? The Verify() vs. Verifable() thing is really confusing.
答
您可以使用 Times.Once()
或 Times.Exactly( 1)
:
mockContext.Verify(x => x.SaveChanges(), Times.Once());
mockContext.Verify(x => x.SaveChanges(), Times.Exactly(1));
下面是对时报类中的方法:
-
ATLEAST
- 指定一个嘲笑方法应该被调用倍为最低 -
AtLeastOnce
- 指定一个嘲笑方法应该被调用一次为最小 -
AtMost
- 指定一个嘲笑方法应该被调用倍时间,最大 -
AtMostOnce
- 指定一个嘲笑方法应该被调用一次作为最大的 -
在
- 指定一个嘲笑方法应该和时间之间调用 -
究竟
- 指定一个嘲笑方法应该精确地调用倍 -
从不
- 指定一个嘲笑的方法不应该调用 -
一旦
- 指定一个嘲笑方法应该精确地调用一次
-
AtLeast
- Specifies that a mocked method should be invoked times times as minimum. -
AtLeastOnce
- Specifies that a mocked method should be invoked one time as minimum. -
AtMost
- Specifies that a mocked method should be invoked times time as maximum. -
AtMostOnce
- Specifies that a mocked method should be invoked one time as maximum. -
Between
- Specifies that a mocked method should be invoked between from and to times. -
Exactly
- Specifies that a mocked method should be invoked exactly times times. -
Never
- Specifies that a mocked method should not be invoked. -
Once
- Specifies that a mocked method should be invoked exactly one time.
请记住,他们是方法调用;我一直得到绊倒,以为他们的属性和遗忘的括号内。
Just remember that they are method calls; I kept getting tripped up, thinking they were properties and forgetting the parentheses.