Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
phpspec-方法的模拟返回值_Php_Phpunit_Phpspec - Fatal编程技术网

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

在phpspec中,我可以模拟方法的返回值吗

例如:

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出现问题,都表明您遇到了设计问题。