views:

136

answers:

1

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?

+1  A: 

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

eu-ge-ne
Ah, thanks! I haven't used a mocking framework before, so I wasn't sure if I had to mock every part of the object.
David Brown
If you are new to Moq then pay attention to difference between MockBehavior.Loose and MockBehavior.Strict. And there is a lot of automatic mocking features in the Moq library.
eu-ge-ne