在单元测试的 Class 中模拟 Class

Mock a Class in a Class in a Unit Test

我在单元测试中有以下代码

using Moq;
using OtherClass;
[TestClass]
public class TestClass
{
    [TestMethod]
    public void TestMethod()
    {
        OtherClass other = new OtherClass();
        OtherClass.foo();
    }
}

这是另一个class

using ThirdClass;
public class OtherClass
{
    public void foo()
    {
        ThirdClass third = new ThirdClass();
        third.bar();
    }
}

ThirdClass 仍在开发中,但我希望能够 运行 我的单元测试使用最小起订量。有没有办法告诉最小起订量在 TestClass 中模拟 ThirdClass 而无需在最小起订量上有 OtherClass use/depend?理想情况下是这样的:

public void TestMethod()
{
    OtherClass other = new OtherClass();
    Mock<ThirdClass> third =  new Mock<ThirdClass>();
    third.setup(o => o.bar()).Returns(/*mock implementation*/);
    /*use third in all instances of ThirdClass in OtherClass*/
    OtherClass.foo();
}

class OtherClass 中的方法 foo() 不可单元测试,因为您创建了真实服务的新实例并且无法模拟它。

如果你想模拟它,那么你必须使用依赖注入来注入 ThirdClass

OtherClass 的示例将是:

public class OtherClass
{
    private readonly ThirdClass _thirdClass;
    public OtherClass(ThirdClass thirdClass) 
    {
         _thirdClass = thirdClass;
    }
    public void foo()
    {
        _thirdClass.bar();
    }
}

你的测试方法和测试其他class的例子可以是:

public void TestMethod()
{
    // Arrange
    Mock<ThirdClass> third =  new Mock<ThirdClass>();
    third.setup(o => o.bar()).Returns(/*mock implementation*/);

    OtherClass testObject= new OtherClass(third);

    // Action
    testObject.foo();

    // Assert
    ///TODO: Add some assertion.
}

您可以使用示例尝试 Unity DI 容器。

谢谢你的想法,伙计们。我最终制作了另一个版本的 OtherClass.foo() ,它接受了一个 ThirdClass 的实例,而在没有它的版本中创建了一个实例。测试时我可以调用 foo(mockThird),但用户可以只使用 foo()。

using ThirdClass;
public class OtherClass
{
    public void foo(ThirdClass third)
    {
        third.bar();
    }
    public void foo()
    {
        foo(new ThirdClass());
    }
}

测试中class

public void TestMethod()
{
    Mock<ThirdClass> third =  new Mock<ThirdClass>();
    third.setup(o => o.bar()).Returns(/*mock implementation*/);
    OtherClass testObject= new OtherClass();

    testObject.foo(third);
}