【发布时间】:2014-08-26 07:13:13
【问题描述】:
我有实现特定接口并为特定数据库扩展基类的存储库类。
基类:
abstract class CouchDB
{
protected $db;
public function __construct(Sag $db)
{
$this->db = $db;
}
private function call_get($url) {...} //this is a wrapper for $db->get()
}
派生类:
class CouchExpression extends CouchDB implements ExpressionInterface
{
public function __construct(Sag $db)
{
parent::__construct($db);
}
}
规格:
class CouchExpressionSpec extends ObjectBehavior
{
public function let($db)
{
$db->beADoubleOf('\Sag');
$this->beConstructedWith($db);
}
public function it_gets_returned_by_its_id()
{
$this->db->get('...')->willReturn([]);
}
}
运行 PHPSpec 时,我得到:property db not found
除了将属性设置为public之外,还有其他方法可以解决此问题吗?
设置为public后我得到:call to a member function willReturn() on a non-object.
所以我不能在$this->db上运行->willReturn()
但是为什么呢?在测试的letmethod 中执行此操作时,它可以工作。但并非所有测试都需要返回相同的数据...
我如何让它运行?
【问题讨论】:
标签: php testing mocking phpspec