简体   繁体   中英

How to mock SignInManager<TUser, TKey>.HasBeenVerifiedAsync() method

Is there a way to mock Microsoft.AspNet.Identity.Owin.SignInManager<TUser, TKey>.HasBeenVerifiedAsync() method?

I am getting this exception when mocking the method

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

Which is OK, I did not know that this method isn't virtual. Is it possible to mock it by its internals, or is there any other workaround?

It's possible to mock Microsoft.AspNet.Identity.Owin.SignInManager<TUser, TKey>.HasBeenVerifiedAsync() with Typemock isolator because it's allows you to mock almost everything also ASP.NET MVC.

Here is an example for a method that uses the HasBeenVerifiedAsync() method (from visual studio MVC template):

[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 });
} 

And this is the test :

[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);
}

You can read more about it here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM