模拟内部函数的响应但测试外部函数

Mock a response of a inner function but test the outer function

我用这种方式设置了 C# 代码。

public class Client : IClient
{
    public string funcA()
    {
       var output = funcB(1);
       //Do something on output and produce finalResult
       return finalResult;
    }

    public string funcB(int x)
    {
         // Some operations on produces string result
         return result;
    }
}

我想模拟 funcB 输出,但让 funcA 根据 funcB 的输出执行。

在我的测试中 class 我做了以下事情:

public class MockClient
{
    private Mock<IClient> _mockClient;

    public MockClient()
    {
        _mockClient = new Mock<IClient>();
    }

    [TestMethod]
    public void TestClient()
    {
        _mockClient.Setup(foo => foo.funcB(It.IsAny<int>())).Returns("test");
        var testOutput = _mockClient.Object.funcA();
    }
}

变量 testOutput returns NULL。我明白为什么,因为对象是从接口创建的。我不确定如何准确解决这个问题。对此的任何输入都会有所帮助。

我假设您使用的是基于语法的 Moq?如果是这样,您可以使用 "Partial Mocks"。示例:

将 funcB 更改为 虚拟

public virtual string funcB(int x)
{
    // Some operations on produces string result
    return result;
}

然后模拟具体类型并将 CallBase 属性 设置为 true:

[TestMethod]
public void TestClient()
{
    Mock<Client> _mockClient = Mock<Client>();
    _mockClient.CallBase = true;
    _mockClient.Setup(foo => foo.funcB(It.IsAny<int>())).Returns("test");
    var testOutput = _mockClient.Object.funcA();
}

上面的示例在 Moq 语法中是完全正确的。但是否使这些功能虚拟化——这是根据您、客户等的目的和需求做出的生产决定。将 funcB 更改为虚拟只是为了测试 - 听起来不合理。

您可以使用 Typemock Isolator 来测试您的原始代码,请参阅:

     public class Client : IClient
     {
        public string funcA()
        {
            var output = funcB(1);
            //Do something on output and produce finalResult
            var finalResult = "B result: " + output;
            return finalResult;
        }

        public string funcB(int x)
        {
            // Some operations on produces string result
            return "result";
        }
    }

    [TestMethod, Isolated]
    public void TestMethod()
    {
        //Arrange
        var foo = new Client();
        Isolate.WhenCalled(() => foo.funcB(0)).WillReturn("test");

       //Act
       var output = foo.funcA();

       //Assert
       Assert.AreEqual("B result: test", output);
   }