如何模拟 SignInManager<TUser, TKey>.HasBeenVerifiedAsync() 方法



有没有模拟Microsoft.AspNet.Identity.Owin.SignInManager<TUser, TKey>.HasBeenVerifiedAsync()方法的方法?

我在嘲笑方法时遇到了这个异常

System.NotSupportedException: Invalid setup on a non-virtual (overridable in VB) member: m => m.HasBeenVerifiedAsync()

这是可以的,我不知道这个方法不是虚拟的。有没有可能通过其内部结构来嘲笑它,或者有没有其他解决方法?

使用Typemock隔离器模拟Microsoft.AspNet.Identity.Owin.SignInManager<TUser, TKey>.HasBeenVerifiedAsync()是可能的,因为它允许您模拟几乎所有的东西,也可以模拟ASP.NET MVC。

下面是一个使用HasBeenVerifiedAsync()方法(来自visual studio MVC模板)的方法示例:

[AllowAnonymous]
public async Task<ActionResult> VerifyCode(string provider, string returnUrl, bool rememberMe)
{
    // Require that the user has already logged in via username/password or external login
    if (!await SignInManager.HasBeenVerifiedAsync())
    {
        return View("Error");
    }
    return View(new VerifyCodeViewModel { Provider = provider, ReturnUrl = returnUrl, RememberMe = rememberMe });
} 

这就是测试:

[TestMethod,Isolated]
public async void TestMethod1()
{
    // Arrange
    AccountController controller = new AccountController();
    // Mocking SignInManager.HasBeenVerifiedAsync()
    Isolate.WhenCalled(() => controller.SignInManager.HasBeenVerifiedAsync()).WillReturn(Task.FromResult(true));
    // Act
    var result = await controller.VerifyCode("tester", "test.com", true) as ViewResult ;
    // Assert
    Assert.AreEqual("tester", (result.Model as VerifyCodeViewModel).Provider);
    Assert.AreEqual("test.com", (result.Model as VerifyCodeViewModel).ReturnUrl);
    Assert.IsTrue((result.Model as VerifyCodeViewModel).RememberMe);
}

你可以在这里阅读更多关于它的信息。

相关内容

  • 没有找到相关文章

最新更新