如何对使用DotNetOpenId的ASP.NET MVC控制器进行单元测试?

时间:2009-06-09 19:56:27

标签: asp.net-mvc unit-testing mocking moq dotnetopenauth

我有一个 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 ,因为它看起来要复杂得多。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

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

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

希望这有帮助