Interfaces for mocking ConfirmEmailAsync and other UserManager methods in MVC5

前端 未结 2 437
被撕碎了的回忆
被撕碎了的回忆 2021-01-20 16:45

I am trying to unit test this controller method, which comes out of the box in current MVC projects.

[AllowAnonymous]
public async Task C         


        
2条回答
  •  耶瑟儿~
    2021-01-20 17:09

    Disclaimer: I work at Typemock.

    Actually you don't need any interface if you are using Typemock, you just need to fake the IdentityResult you require and change the behavior of the asynchronous method "ConfirmEmailAsync", for example a test that checks the scenario of an Unconfirmed email:

    [TestMethod, Isolated]
    public async Task TestWhenEmailIsBad_ErrorMessageIsShown()
    {
    
        // Arrange
        // Create the wanted controller for testing and fake IdentityResult
        var controller = new aspdotNetExample.Controllers.AccountController();
        var fakeIdentityRes = Isolate.Fake.Instance();
    
        // Fake HttpContext to return a fake ApplicationSignInManager
        var fakeSIM = Isolate.WhenCalled(() => controller.UserManager).ReturnRecursiveFake();
    
        // Modifying the behavior of ConfirmEmailAsync to return fakeIdentityRes
        Isolate.WhenCalled(() => fakeSIM.ConfirmEmailAsync("", "")).WillReturn(Task.FromResult(fakeIdentityRes));
        Isolate.WhenCalled(() => fakeIdentityRes.Succeeded).WillReturn(false);
    
        // Act
        var result = await controller.ConfirmEmail("", "") as ViewResult;
    
        // Assert
        Assert.AreEqual("Error", result.ViewName);
    }    
    

提交回复
热议问题