1

如果我有两个班级:ClassA 和 ClassB。

ClassA 调用 ClassB 上的公共方法,即在 ClassA 中:

public function foo()
{
    $this->classB->bar();
}

我有涵盖 foo 的测试 - 但我是否应该始终在 b 类中编写涵盖 bar 的测试?

鉴于代码是直接测试的,我不确定这是否值得?

4

3 回答 3

1

The other way to look at it is in terms of the responsibility of the class.
That responsibility is what should be Asserted with tests.

Class can be ClassA or ClassB etc
Write Tests to assert responsibilities of Class A
Write Tests to assert responsibilities of Class B

Combine that with the view that there will a dependencies and object maps
You l naturally cover tests for your dependencies and then higher up the object map tracing to the root

In short
Class B needs to have tests
Class A which depends on some capability of Class B needs tests of its own

Note: the responsibility/capability of each of them will be different

于 2013-11-04T09:58:43.563 回答
0

是的,写测试。否则, classB::bar() 不可重用(甚至不可使用。)

如果 bar 是 ClassA 的私有方法,则不必这样做。

于 2013-11-03T02:35:34.647 回答
0

是的,你也应该测试吧。您所有的类都应该有自己的测试用例,您可以在其中测试它们的公共方法。因此,您应该有一个单独的 classB 测试用例,您可以在其中测试方法 bar()。

这样做的原因是,当方法 bar()(或 bar() 调用的 classB 的另一个方法)中发生错误时,您的测试将在方法 foo() 中失败,这可能会导致错误并导致更多时间来查找方法 bar() 中的实际错误。在您的示例中可能不会出现问题,但在现实世界中,您会测试更复杂的代码。

另一个原因是测试的复杂性。您的测试应该在所有可能的情况下测试您的方法。可以说,函数 bar() 可以在 5 种不同的情况下进行测试,而 foo() 也可以在其自己的 5 种情况下进行测试。如果您只测试 foo(),则必须编写一个测试来覆盖 5*5 = 25 个案例。如果 foo() 更复杂,并且调用了另一个方法,假设 classB->bar2() 有另外 5 个不同的案例,你将不得不编写一个涵盖 5*5*5 = 125 个案例的测试!编写 3 个测试(每个 echach 5 个案例)比编写 1 个测试覆盖 125 个案例要容易得多。这 3 个测试也更容易阅读和维护。

于 2013-11-03T09:28:31.607 回答