次のようなモックメソッドがあります。
class NotMineClass {
T Execute(Func operation)
{
// do something
return operation();
}
}
私のコードでは、次のようにします。
public MyType MyMethod()
{
MyType object = new MyType();
bool success = notMineClassInstance.Execute(() =>
{
// some things
retVal = injectedObject1.method();
object.attribute = injectedObject2.method();
// some other things
return retVal;
}
if (success)
{
object.otherAttribute = someValue;
}
return object;
}
私の場合、MyMethod を Moq でテストしており、Func の動作が期待どおりであることを確認したいと考えています。私はその本体にいくつかの注入されたオブジェクトを持っています。これはモックであり、検証する必要があります。また、戻り値の作成も開始されるため、パラメーターとして渡された関数を呼び出さない限り、アサーションを行うことはできません。
Java と jUnit + EasyMock では、渡されたパラメーターを次のようにキャプチャします。
public void testMyMethod() {
// ...
Capture < Function < void, Boolean > > functionCapture = Captures.last();
expect(notMineClassInstance.execute(EasyMock.capture(functionCapture)));
// Expectations for the body of the function
replay();
functionCapture.getValue().apply(null);
}
C# + Moq を使用して同じことを行うにはどうすればよいですか?