2009-05-04 8 views
1

私はDotNetOpenAuth(正式にはDotNetOpenIdとして知られている)Responseオブジェクトを取り入れています。私のメソッドは、要求されたデータを抽出し、このユーザーがシステムに存在するかどうかを確認します。yadda yadda yadda ...終了したらauth'dユーザーインスタンスを返します。このDotNetOpenAuth応答オブジェクトをどのようにmoqするのですか?

ここでは、moqを使用して、この応答オブジェクトをモックアップして、認証方法(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
Responseオブジェクト:DotNetOpenAuthフレームワークから取られたが

答えて

2

私は特に、部品番号に慣れていないんだけど、レスポンスオブジェクトがあります同じインタフェースを実装し、同じ種類の値を返す準備ができているクラスを作成することで簡単に嘲笑することができます。

...ちょうど部品番号をダウンロードし、そのよう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); 

ではなく、あなたがテストしているメソッドにrespオブジェクトを渡したいと思うだろうConsole.WriteLineした結果を送信します。

+0

こんにちはAndrew - ライブラリが大好き!だから、私はIAuthenticationResponseをimpliments TestAuthenticationResponseオブジェクトを作成する必要がありますし、私はさまざまなフィールドを期待してハードコード? –

+1

ありがとうございます。あなたは...私はMoqを試してみましたが、インタフェースを自分で実装するこのクラスを構築する必要がないことを示すために私の答えを更新しましたが。楽しい。 –

+0

<3 Andrew :)このすべてのMoqのものは私を完全に混乱させます:(したがって、私はdl'ing moqなどであなたの時間を誠実に感謝します。将来MVCサンプルRPプロジェクトでMoqテストが見えますか? DotNotOpenAuth Visual Studion solution)?:) –

関連する問題