2012-01-24 5 views
3

私はまだRhinoモックを学んでおり、それについて質問があります。 - たとえば、私は嘲笑インタフェースの機能を持っている:あなたは関数「FunctionGetCollection」を参照したようRhino Mocks - ref/out引数付きのモッキングコレクション


    public interface ISomeObject 
    { 
     string Name {get; set;} 
     int Id {get;set;} 
    } 

    // This class will be returned as and answer to function call 
    public class AnswerObject 
    { 
     public bool IfError {get;set;} 
    } 

    // Main interface 
    public interface IClass 
    { 
     AnswerObject FunctionGetCollection(ref ICollection <ISomeObject> ListOfInternalObjects, ref int Number); 
    } 



「を参照」として渡された2つのパラメータを受け取ると「機能-答え」として別のクラスを返します。このファンクションをスタブするのを手伝ってもらえますか?私が使用できるようにする必要があります。

  • 関数は、構文は非常に素晴らしいではありません

答えて

4

異なるAnswerObjectに戻ります

  • 機能(ないパラメータにコード内の所定の位置に基づいて)別のコレクションを返します。頻繁に使用されることはなく、旧式のRhino.Mocks.Constraintsを使用します。

    このコードは、すべてのref-argumentsを新しい値に置き換える模擬を設定します。

    AnswerObject answerObject; 
    ICollection <ISomeObject> collection; 
    int number; 
    
    IClass iClassMock = MockRepository.GenerateMock<IClass>(); 
    iClassMock 
        .Stub(x => x.FunctionGetCollection(
        ref Arg<ICollection <ISomeObject>>.Ref(Is.Anything(), collection).Dummy, 
        ref Arg<int>.Ref(Is.Anything(), number).Dummy); 
        .Return(answerObject); 
    

    あなたはそれらがモックに渡される値を保持したい場合は、あなたがWhenCalledブロックでこれを実装する必要があります。

    iClassMock 
        .Stub(x => x.FunctionGetCollection(
        ref Arg<ICollection <ISomeObject>>.Ref(Is.Anything(), null).Dummy, 
        ref Arg<int>.Ref(Is.Anything(), 0).Dummy); 
        .WhennCalled(call => 
        { 
        // reset the ref arguments to what had been passed to the mock 
        // not sure if it also works with the int 
        call.Arguments[0] = call.Arguments[0]; 
        call.Arguments[1] = call.Arguments[1]; 
        }) 
        .Return(answerObject); 
    
  • +0

    Is.Anythingは - エラーを与える: 引数1を: 'メソッドグループ'から 'Rhino.Mocks.Constraints.AbstractConstraint'に変換できません (関数の必要条件としてArg <>パラメータの前に 'ref'を追加してください) :(: – Jasper

    +0

    これは正しい答えです.Anything()を両方の場所に追加し、 'ref '' Arg <> 'の前にあり、それは動作します!私はとにかく閉じていた:) :) – Jasper

    +0

    正確に。それを私が直した。 –

    関連する問題