Md.*_*lam 6 php phpunit php-traits
我有一个特征(由另一个特征使用)和一个非抽象方法,我需要模拟,但是当我尝试这样做时,我收到错误消息:
尝试
getOfficeDays配置由于不存在、未指定、最终或静态而无法配置的方法
这是我要测试的代码:
trait OfficeDaysTrait {
public function getOfficeDays(): array {
// This normally calls the database, that's why I want to mock it in tests
return [
[
'day' => 'mon',
'openingTime' => '08:00:00',
'closingTime' => '17:00:00'
]
];
}
}
trait EmployeeAttendenceTrait {
use OfficeDaysTrait;
public function isLate($today)
{
$late = false;
$officeDays = $this->getOfficeDays();
// compare logic with today.. whether the employee is late or not
return $late;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的测试方法主体:
$mock = $this->getMockForTrait(EmployeeAttendenceTrait::class);
$mock->expects($this->once())
->method('getOfficeDays')
->will([])
;
$this->assertTrue($mock->isLate());
Run Code Online (Sandbox Code Playgroud)
此语法基于phpunit 测试特征文档中显示的示例
为什么我会收到此错误消息,我该如何模拟getOfficeDays才能测试我的isLate方法?
AD7*_*six 10
我将在下面详细介绍,但是:
$mock = $this->getMockForTrait(EmployeeAttendenceTrait::class);
Run Code Online (Sandbox Code Playgroud)
嘲笑没有方法,$mock因此相当于:
class Testing extends PHPUnitsInternalMockStub
{
use EmployeeAttendenceTrait;
}
$mock = new Testing;
Run Code Online (Sandbox Code Playgroud)
错误信息如下:
尝试配置方法 getOfficeDays 无法配置,因为它不存在、尚未指定、是最终的或静态的
我们来看看每一种可能性(几乎依次)
代码看起来清楚地有一个名为的方法getOfficeDays- 如果有疑问,使用get_class_methods会澄清:
<?php
use PHPUnit\Framework\TestCase;
class SOTest extends TestCase
{
public function testMethods()
{
$mock = $this->getMockForTrait(EmployeeAttendenceTrait::class);
print_r(get_class_methods($mock));
}
}
Run Code Online (Sandbox Code Playgroud)
哪些输出:
R 1 / 1 (100%)Array
(
[0] => __clone
[1] => expects
[2] => method
[3] => __phpunit_setOriginalObject
[4] => __phpunit_getInvocationMocker
[5] => __phpunit_hasMatchers
[6] => __phpunit_verify
[7] => isLate
[8] => getOfficeDays
)
Time: 569 ms, Memory: 12.00MB
There was 1 risky test:
1) SOTest::testMethods
This test did not perform any assertions
Run Code Online (Sandbox Code Playgroud)
所以不是这样。
public function getOfficeDays(): array {
Run Code Online (Sandbox Code Playgroud)
方法签名显然没有将方法声明为 final - 所以不是这样。
public function getOfficeDays(): array {
Run Code Online (Sandbox Code Playgroud)
通过逻辑推演这有是问题-它是唯一一个需要使用的PHPUnit的一些理解。
getMockForTrait的文档阅读(强调添加):
getMockForTrait() 方法返回一个使用指定特征的模拟对象。给定特征的所有抽象方法都被模拟。这允许测试特征的具体方法。
由于所有 trait 方法都不是抽象的,因此预计不会使用问题中的语法来模拟任何方法。更深入地看,getMockForTrait的方法签名有更多参数:
/**
* Returns a mock object for the specified trait with all abstract methods
* of the trait mocked. Concrete methods to mock can be specified with the
* `$mockedMethods` parameter.
*
* @throws RuntimeException
*/
public function getMockForTrait(
string $traitName,
array $arguments = [],
string $mockClassName = '',
bool $callOriginalConstructor = true,
bool $callOriginalClone = true,
bool $callAutoload = true,
array $mockedMethods = [],
bool $cloneArguments = true): MockObject
Run Code Online (Sandbox Code Playgroud)
有一个参数用于指定显式模拟方法,因此编写测试如下:
class SOTest extends TestCase
{
public function testMethods()
{
$mock = $this->getMockForTrait(
EmployeeAttendenceTrait::class,
[], # These
'', # are
true, # the
true, # defaults
true, #
['getOfficeDays']
);
$mock->expects($this->once())
->method('getOfficeDays')
->willReturn([]);
$today = new DateTime(); // or whatever is appropriate
$this->assertTrue($mock->isLate($today));
}
}
Run Code Online (Sandbox Code Playgroud)
将生成一个有效的模拟对象并允许测试运行。
| 归档时间: |
|
| 查看次数: |
1132 次 |
| 最近记录: |