相关疑难解决方法(0)

phpunit mock方法使用不同的参数进行多次调用

有没有办法为不同的输入参数定义不同的模拟期望?例如,我有一个名为DB的数据库层类.此类具有名为"Query(string $ query)"的方法,该方法在输入时采用SQL查询字符串.我可以为此类(DB)创建模拟,并为依赖于输入查询字符串的不同Query方法调用设置不同的返回值吗?

php phpunit mocking

107
推荐指数
4
解决办法
7万
查看次数

PHPUnit中的模拟 - 使用不同参数的相同方法的多个配置

是否可以以这种方式配置PHPUnit mock?

$context = $this->getMockBuilder('Context')
   ->getMock();

$context->expects($this->any())
   ->method('offsetGet')
   ->with('Matcher')
   ->will($this->returnValue(new Matcher()));

$context->expects($this->any())
   ->method('offsetGet')
   ->with('Logger')
   ->will($this->returnValue(new Logger()));
Run Code Online (Sandbox Code Playgroud)

我使用PHPUnit 3.5.10,当我要求Matcher时它会失败,因为它需要"Logger"参数.这就像第二个期望是重写第一个,但是当我转储模拟时,一切看起来都不错.

php phpunit mocking

49
推荐指数
4
解决办法
2万
查看次数

替换 PHPUnit 方法 `withConsecutive` (在 PHPUnit 10 中废弃)

由于该方法withConsecutive将在 PHPUnit 10 中被删除(在 9.6 中已弃用),我需要将此方法的所有出现替换为新代码。

尝试寻找一些解决方案,但没有找到任何合理的解决方案。

例如,我有一个代码

    $this->personServiceMock->expects($this->exactly(2))
        ->method('prepare')
        ->withConsecutive(
            [$personFirst, $employeeFirst],
            [$personSecond, $employeeSecond],
        )
        ->willReturnOnConsecutiveCalls($personDTO, $personSecondDTO);
Run Code Online (Sandbox Code Playgroud)

我应该替换哪个代码withConsecutive

PS官方网站上的文档仍然显示了如何使用withConsecutive

testing phpunit deprecated

30
推荐指数
2
解决办法
1万
查看次数

标签 统计

phpunit ×3

mocking ×2

php ×2

deprecated ×1

testing ×1