【发布时间】:2015-07-08 00:16:43
【问题描述】:
我是 nSubstitute 的新手。我在很多文章中都看到过这样的代码示例。测试接口有什么用?以下测试何时会失败?根据IMathService的实际实现,下面的测试会失败吗?这里的想法是先在 TDD 中编写它,然后删除替代项并放入真正的实现?或者这个想法是测试一些真正的实现,但用 Nsubstitute 替换依赖项?如果是,则此示例没有显示。我很困惑。
public interface IMathService
{
int Add(int a, int b);
}
[Test]
public void nSubstituteMockingInterface()
{
var mathService = Substitute.For<IMathService>();
mathService.Add(2,3).Returns(4);
Assert.IsTrue(mathService.Add(2, 3) == 4);
}
假设我有以下实现,上面的测试会失败吗? Substitute.For() 行将返回 MyMathService 的实例?我相信,不是。如果是,如果我有多个 IMathService 实现会发生什么
class MyMathService: IMathService
{
public int Add(int a, int b)
{
throw new Exception("error");
}
}
【问题讨论】:
标签: c# unit-testing nsubstitute