phpspec-方法的模拟返回值
在phpspec中,我可以模拟方法的返回值吗 例如:phpspec-方法的模拟返回值,php,phpunit,phpspec,Php,Phpunit,Phpspec,在phpspec中,我可以模拟方法的返回值吗 例如: class MyClass() { public function getStaffMemberNames() { // db call to get array of staff member names } public function sortStaffMemberNames() { return sort($this->getStaffMemberNam
class MyClass()
{
public function getStaffMemberNames()
{
// db call to get array of staff member names
}
public function sortStaffMemberNames()
{
return sort($this->getStaffMemberNames());
}
}
我对测试sortStaffMemberNames方法感兴趣。但它依赖于另一个使用db连接的类方法。我想模拟getStaffMemberNames,以便轻松测试
如何实现这一点?phpspec中没有部分模拟(您不能模拟测试中的类)。这是个坏习惯
您应该模拟协作者(例如数据库连接) 坏习惯?因此,最好在使用getStaffMemberNames()方法的每个方法中模拟数据库调用?phpspec尝试实施一些实践,其中包括单一责任原则。在您的情况下,“sortStaffMemberNames()”有它自己的职责(排序),应该被提取到一个新对象中,并作为参数接收“getStaffMemberNames”的结果。无论何时phpspec出现问题,都表明您遇到了设计问题。