假设我们有一个方法,主要将各种其他方法连接在一起;并没有做很多自己的逻辑。假设它是服务层中服务类的典型成员,调用域模型(业务)类、数据访问类和电子邮件实用程序/服务类中的方法。
我发现为此方法编写真正的单元测试需要大量的测试双重设置,并且这里测试的逻辑很少。如果我继续这样做的话,似乎成本将超过值(value)。我该怎么办?
最佳答案
每个单元测试不应有超过一个模拟。来自 Roy Osherove 的单元测试的艺术(第 94 页):
In a test where you test only one thing (which is how I recommend you write tests), there should be no more than one mock object. All other fake objects will act as stubs. Having more than one mock per test usually means you’re testing more than one thing, and this can lead to complicated or brittle tests.
Here是一个相关的堆栈溢出问题,可能会有所帮助。
关于unit-testing - 我应该如何使用大量 DOC(依赖代码)对方法进行单元测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4147036/