如何验证使用 Moq 的方法只调用了一次?Verify() 与 Verifable() 的事情确实令人困惑.
你可以使用Times.Once(),或者Times.Exactly(1):p>
mockContext.Verify(x => x.SaveChanges(), Times.Once());mockContext.Verify(x => x.SaveChanges(), Times.Exactly(1));以下是 Times 类的方法:p>
AtLeast - 指定模拟方法的调用次数最少.AtLeastOnce - 指定至少应调用一次模拟方法.AtMost - 指定模拟方法的调用次数最多.AtMostOnce - 指定一个模拟的方法最多应该被调用一次.Between - 指定应在 from 和 to 时间之间调用模拟方法.Exactly - 指定一个模拟方法应该被准确地调用几次.Never - 指定不应调用模拟方法.Once - 指定一个模拟方法应该被调用一次.记住它们是方法调用;我一直被绊倒,以为它们是属性而忘记了括号.
How do I verify a method was called exactly once with Moq? The Verify() vs. Verifable() thing is really confusing.
You can use Times.Once(), or Times.Exactly(1):
mockContext.Verify(x => x.SaveChanges(), Times.Once());
mockContext.Verify(x => x.SaveChanges(), Times.Exactly(1));
Here are the methods on the Times class:
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.
这篇关于如何验证使用 Moq 仅调用了一次方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
ASP.NET Core 使用 Azure Active Directory 进行身份验证并ASP.NET Core authenticating with Azure Active Directory and persisting custom Claims across requests(ASP.NET Core 使用 Azure Active Directory 进行身
ASP.NET Core 2.0 Web API Azure Ad v2 令牌授权不起作用ASP.NET Core 2.0 Web API Azure Ad v2 Token Authorization not working(ASP.NET Core 2.0 Web API Azure Ad v2 令牌授权不起作用)
ASP Core Azure Active Directory 登录使用角色ASP Core Azure Active Directory Login use roles(ASP Core Azure Active Directory 登录使用角色)
如何获取守护进程或服务器到 C# ASP.NET Web API 的How do I get Azure AD OAuth2 Access Token and Refresh token for Daemon or Server to C# ASP.NET Web API(如何获取守护进程或服务器到 C# ASP.N
.Net Core 2.0 - 获取 AAD 访问令牌以与 Microsoft Graph.Net Core 2.0 - Get AAD access token to use with Microsoft Graph(.Net Core 2.0 - 获取 AAD 访问令牌以与 Microsoft Graph 一起使用)
异步调用时 Azure KeyVault Active Directory AcquireTokenAAzure KeyVault Active Directory AcquireTokenAsync timeout when called asynchronously(异步调用时 Azure KeyVault Active Directory AcquireTokenAsync 超