vendredi 5 février 2016

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

I have a C# code setup this way.

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

I want to mock funcB output but let the funcA perform as is based on the output from funcB.

In my test class I do the following:

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

The variable testOutput returns NULL. I understand why, because the object is created from an Interface. I am not sure how to exactly work around this problem. Any inputs on this will be helpful.

Aucun commentaire:

Enregistrer un commentaire