How do I unit test an ASP.NET MVC controller that uses DotNetOpenId?
-
13-09-2019 - |
Question
I have an AccountController whose constructor takes an object derived from my custom IOpenIdAuthentication interface. By default, this is an OpenIdAuthenticationService object that wraps an OpenIdRelyingParty. The interface looks like this:
public interface IOpenIdAuthentication {
IAuthenticationResponse Response { get; }
IAuthenticationRequest CreateRequest(string identifier);
}
I can mock IAuthenticationResponse:
_mockResponse = new Mock<IAuthenticationResponse>(MockBehavior.Loose);
_mockResponse.SetupGet(r => r.ClaimedIdentifier).Returns(identifier);
_mockResponse.SetupGet(r => r.Status)
.Returns(AuthenticationStatus.Authenticated);
// ... removed the formatting of 'friendlyId' ...
_mockResponse.SetupGet(r => r.FriendlyIdentifierForDisplay).Returns(friendlyId);
However, I'm not sure how to mock IAuthenticationRequest as it appears much more complicated. Any ideas?
Solution
It's not much more complicated. If you are doing only authentication, then mocking RedirectToProvider() would be enough. In the simplest case it looks like:
_mockRequest = new Mock<IAuthenticationRequest>(MockBehavior.Strict);
_mockRequest.Setup(r => r.RedirectToProvider());
Hope this helps
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow