David Brown
David Brown

Reputation: 36239

How do I unit test an ASP.NET MVC controller that uses DotNetOpenId?

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?

Upvotes: 1

Views: 266

Answers (1)

eu-ge-ne
eu-ge-ne

Reputation: 28153

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

Upvotes: 1

Related Questions