1

我有一个AccountController,它的构造函数采用从我的自定义IOpenIdAuthentication接口派生的对象。默认情况下,这是一个包装OpenIdRelyingParty的OpenIdAuthenticationService对象。界面如下所示:

public interface IOpenIdAuthentication {
    IAuthenticationResponse Response { get; }
    IAuthenticationRequest CreateRequest(string identifier);
}

我可以模拟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);

但是,我不确定如何模拟IAuthenticationRequest,因为它看起来要复杂得多。有任何想法吗?

4

1 回答 1

1

这并不复杂。如果您只进行身份验证,那么模拟 RedirectToProvider() 就足够了。在最简单的情况下,它看起来像:

_mockRequest = new Mock<IAuthenticationRequest>(MockBehavior.Strict);
_mockRequest.Setup(r => r.RedirectToProvider());

希望这可以帮助

于 2009-06-09T20:37:39.670 回答