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

时间:2021-03-22 01:44:23

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:

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

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

I can mock IAuthenticationResponse:

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

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

1 个解决方案

#1


1  

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:

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

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

Hope this helps

希望这可以帮助

#1


1  

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:

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

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

Hope this helps

希望这可以帮助