【发布时间】:2015-06-24 18:06:33
【问题描述】:
假设我有以下课程。
class SomeClass {
public function shortcutMethod($arg1) {
return $this->method($arg1, 'something');
}
public function method($arg1, $arg2) {
// some stuff
}
}
所以shortcutMethod 是另一种方法的快捷方式。假设我想编写一个给定的测试,$arg1 shortcutMethod 将使用正确的参数正确调用 method。
到目前为止,我认为我需要模拟该类以期望使用一些参数调用method,然后像这样在模拟对象上调用shortcutMethod (注意我使用的是 Mockery) em>。
$mock = m::mock("SomeClass");
$mock = $mock->shouldReceive('method')->times(1)->withArgs([
'foo',
'something'
]);
$mock->shortcutMethod('foo');
这会导致类似shortcutMethod() does not exist on this mock object 的异常。
我误解了 mocking 的用法吗?我知道对于依赖注入到类中的对象更有意义,但是在这种情况下呢?你会怎么做?也许更重要的是,这种测试没有用吗?如果有,为什么?
【问题讨论】:
标签: unit-testing phpunit mockery