9

我有一个看起来像这样的模拟方法:


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 做同样的事情?

4

1 回答 1

16

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 时,您还需要测试案例。

于 2012-04-18T19:37:26.517 回答