문제

나는 a를 취하는 방법이있다 dotnetopenauth (공식적으로 dotnetopenid로 알려짐) Response 물체. 내 방법은 청구 된 데이터를 추출하고,이 사용자가 시스템 에이 사용자가 존재하는지 확인하여 Yadda Yadda Yadda ... 및 완료되면 인증 사용자 인스턴스를 반환합니다.

이제 .. 어떻게 사용할 수 있습니까? 모크 이 응답 객체를 조롱하려면 인증 방법을 테스트합니다. AuthenticateUser() )?

switch (response.Status)
{
    case AuthenticationStatus.Authenticated:

    User user = null;
    try
    {
        // Extract the claimed information and 
        // check if this user is valid, etc.
        // Any errors with be thrown as Authentication Errors.
        user = _authenticationService.AuthenticateUser(response) as User;
    }
    catch (AuthenticationException exception)
    {
        ViewData.ModelState.AddModelError("AuthenticationError", exception);
    }

    .. other code, like forms auth, other response.status' etc. ..
}

조롱 프레임 워크 : Moq
언어 : .NET C# 3.5 SP1
응답 개체 : Dotnetopenauth 프레임 워크에서 가져온

도움이 되었습니까?

해결책

특히 MOQ에 익숙하지 않지만 응답 객체는 구현되는 유형입니다. DotNetOpenAuth.OpenId.RelyingParty.IAuthenticationResponse, 따라서 동일한 인터페이스를 구현하고 동일한 종류의 값을 반환 할 준비가 된 클래스를 만들어 쉽게 조롱 할 수 있습니다.

... 방금 MOQ를 다운로드하고 다음과 같은 IAuthenticationResponse를 조롱했습니다.

var response = new Mock<IAuthenticationResponse>(MockBehavior.Loose);
response.SetupGet(r => r.ClaimedIdentifier)
        .Returns("http://blog.nerdbank.net/");
response.SetupGet(r => r.Status)
        .Returns(AuthenticationStatus.Authenticated);
response.SetupGet(r => r.FriendlyIdentifierForDisplay)
        .Returns("blog.nerdbank.net");

IAuthenticationResponse resp = response.Object;
Console.WriteLine(resp.ClaimedIdentifier);

결과를 보내기보다는 분명히 Console.WriteLine 당신은 통과하고 싶을 것입니다 resp 테스트중인 메소드에 반대합니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top