0

Simply passing Mock HttpContext to the controller context while unit testing logout functionality will throw following error HttpContext.Signout + value can not be null (Parameter 'provider')

var mockHttpContext = new Mock<HttpContext>();

       

//set the controller HttpContext

_controller.ControllerContext.HttpContext = mockHttpContext.Object;

// Now call the Logout action method and Test result

Debanjan
  • 69
  • 1
  • 8

1 Answers1

1

If you drill down to the definition of SigOutAsync code it expect Authentication Service Provider which should not be null . Hence , along with mocking HttpContext you also need to mock the service provider which will solve the issue .

       //mock the Http Context along with Service provider
        var mockHttpContext = new Mock<HttpContext>();
        var authServiceMock = new Mock<IAuthenticationService>();
        authServiceMock.Setup(_ => _.SignOutAsync(It.IsAny<HttpContext>(), It.IsAny<string>(),It.IsAny<AuthenticationProperties>())).Returns(Task.FromResult((object)null));
        var serviceProviderMock = new Mock<IServiceProvider>();
        serviceProviderMock.Setup(_ => _.GetService(typeof(IAuthenticationService)))
            .Returns(authServiceMock.Object);
        mockHttpContext.Setup(x => x.RequestServices)
          .Returns(serviceProviderMock.Object);

        //set the controller HttpContext
        _controller.ControllerContext.HttpContext = mockHttpContext.Object;
       // Now call the Logout and Test

Similar logic can be applied while testing SignIn functionality

Refer here as well- How to unit test HttpContext.SignInAsync()?

Debanjan
  • 69
  • 1
  • 8