【发布时间】:2017-05-12 02:47:57
【问题描述】:
我在单元测试中有以下代码
using Moq;
using OtherClass;
[TestClass]
public class TestClass
{
[TestMethod]
public void TestMethod()
{
OtherClass other = new OtherClass();
OtherClass.foo();
}
}
这是另一个类
using ThirdClass;
public class OtherClass
{
public void foo()
{
ThirdClass third = new ThirdClass();
third.bar();
}
}
ThirdClass 仍在开发中,但我希望能够使用 moq 运行我的单元测试。有没有办法告诉 moq 在 TestClass 中模拟 ThirdClass 而不让 OtherClass 使用/依赖于 moq?理想情况下是这样的:
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();
}
【问题讨论】:
-
听起来像
OtherClass应该提供ThirdClass的一个实例,而不是创建一个。
标签: c# unit-testing mocking moq