我有一个单元测试模拟的类在单元测试
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();
}
}
三级栏目仍处于开发阶段,但我想一个类能够使用moq运行我的单元测试。有没有办法告诉moq模拟TestClass中的ThirdClass而不使用其他类使用/取决于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'代替*创建*之一。 – David