1

Rhino でモックされたオブジェクトがあり、モックされているインターフェイスのメソッドを表す MethodInfo があります。MethodInfo で表されるメソッドがモック オブジェクトで呼び出されたかどうかを確認したいと思います。

コンパイル時にメソッドを知っていれば、Rhino の AssertWasCalled() メソッドを使用するでしょう。

1 日か 2 日余裕があれば、式ツリーの魔法を使ってコードを生成するかもしれませんが、それはこの問題の現在の価値を超えています。

もっと簡単な方法を逃したのではないかと思っています。どんなアイデアでも大歓迎です。

4

2 に答える 2

1

ここで式ツリーを使用すると物事が複雑になりすぎると思われますが、私は同意しません。

// What you have:
var methodInfo = typeof(ICloneable).GetMethod("Clone");

var parameter = Expression.Parameter(typeof(ICloneable), "p");
var body = Expression.Call(parameter, methodInfo);

// Rhino can accept this as an expectation.
var lambda = Expression.Lambda<Action<ICloneable>>(body, parameter).Compile();

そして、次のように使用します。

var clone = new MockRepository().Stub<ICloneable>();
clone.Replay();
clone.Clone();
clone.AssertWasCalled(lambda);
于 2012-08-21T14:11:44.343 に答える
0

上記のアニの有益なコメントに基づいて、次のようになりました。

GetParameters()[0]これはテスト コードなので、恐怖について楽観的です!

    protected static object InvokeOldOperation<TCurrentInterface>(MethodInfo oldOperationMethod, object implToTest, TCurrentInterface mockCurrentImpl)
    {
        MethodInfo currentInterfaceMethod = typeof(TCurrentInterface).GetMethod(oldOperationMethod.Name);

        Type oldRequestType = oldOperationMethod.GetParameters()[0].ParameterType;
        var request = Activator.CreateInstance(oldRequestType);

        Type currentRequestType = currentInterfaceMethod.GetParameters()[0].ParameterType;

        ParameterExpression instanceParameter = Expression.Parameter(typeof(TCurrentInterface), "i");
        var requestParameter = Expression.Constant(null, currentRequestType);
        MethodCallExpression body = Expression.Call(instanceParameter, currentInterfaceMethod, new Expression[] { requestParameter});
        var lambda = Expression.Lambda<Func<TCurrentInterface, object>>(body, instanceParameter).Compile();

        var response = oldOperationMethod.Invoke(implToTest, new[] {request});
        mockCurrentImpl.AssertWasCalled(lambda, opt => opt.Constraints(Is.TypeOf(currentRequestType)).Repeat.Once());

        return response;
    }
于 2012-08-21T15:08:21.753 に答える