使用Moq和C#调用Func作为参数传递给模拟

时间:2012-04-18 18:16:39

标签: c# .net moq capture xunit

我有一个看似这样的模拟方法:


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;
}

我的情况是,我正在使用Moq测试MyMethod,我想验证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进行相同操作?

1 个答案:

答案 0 :(得分:10)

为方法提供Returns时,您可以捕获调用参数:

Mock<NotMineClassInstance> mock = new Mock<NotMineClassInstance>();
mock.Setup(x => x.Execute<bool>(It.IsAny<Func<bool>>()))
    .Returns((Func<bool> captured) => { captured(); return true; });

以下是您的代码的完整测试:

[Test]
public void TestingSomething()
{
    // Arrange
    Mock<NotMineClassInstance> mockNotMine = new Mock<NotMineClassInstance>();
    mockDep.Setup(x => x.Execute<bool>(It.IsAny<Func<bool>>())).Returns((Func<bool> func) => func());

    Mock<Injected1> mockInjected1 = new Mock<Injected1>();
    mockInjected1.Setup(i => i.Method()).Returns(true);

    Mock<Injected2> mockInjected2 = new Mock<Injected2>();
    mockInjected2.Setup(i => i.Method()).Returns("xxx");

    YourClass yourObject = new YourClass(mockDep.Object, mockInjected1.Object, mockInjected2.Object);

    // Act
    MyType my = yourObject.MyMethod();    

    // Assert
    mockNotMine.Verify(d => d.Execute<bool>(It.IsAny<Func<bool>>()));
    mockInjected1.Verify(i => i.Method());
    mockInjected2.Verify(i => i.Method());

    Assert.That(my.Attribute, Is.EqualTo("xxx"));
    Assert.That(my.OtherAttribute, Is.EqualTo(someValue));            
}

当mockInjected1.Method返回false时,你还需要测试大小写。