【发布时间】:2021-02-25 23:29:21
【问题描述】:
我有一堂课如下:
class Foo extends BaseObject {
public static function grab() {
// do some DB search and return an instance of self
// with the data in the object
return new Foo($id);
}
public function insert() {
// insert the data in the database for this object
return false;
}
public function delete() {
// delete the data from the database for this object
return false;
}
}
如何模拟此对象,同时仍保留断言对 insert 和 delete 的调用按预期运行的能力?
我所做的是:use AspectMock\Test;
$mock = Test::double('Foo', [
'grab' => Stub::make('Foo'),
'insert' => true,
'delete' => true,
]);
// ... later in the test ...
self::assertSame($foo->insert(), true);
$mock->verifyInvoked('insert', 1);
这绕过了 insert 和 update 方法的模拟,并返回 false 而不是预期的 true。它也不会按预期计算调用。
那么我怎样才能让 mock 自己返回呢?
(请原谅我可能在mock 和stub 之间造成的任何混淆)
【问题讨论】:
标签: php unit-testing mocking phpunit codeception