php - 如何使用 phpunit 模拟非抽象特征方法?
问题描述
我有一个需要模拟的非抽象方法的特征(由另一个特征使用),但是当我尝试这样做时,我收到错误消息:
尝试
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;
}
}
这是我的测试方法主体:
$mock = $this->getMockForTrait(EmployeeAttendenceTrait::class);
$mock->expects($this->once())
->method('getOfficeDays')
->will([])
;
$this->assertTrue($mock->isLate());
为什么我会收到此错误消息,我该如何模拟getOfficeDays
才能测试我的isLate
方法?
解决方案
没有被mock的方法不能有期望
我将在下面详细介绍,但这个:
$mock = $this->getMockForTrait(EmployeeAttendenceTrait::class);
没有嘲笑任何方法,$mock
因此相当于:
class Testing extends PHPUnitsInternalMockStub
{
use EmployeeAttendenceTrait;
}
$mock = new Testing;
检查错误信息
错误消息如下:
尝试配置方法 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));
}
}
哪个输出:
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
所以不是这样。
是最终的
public function getOfficeDays(): array {
方法签名显然没有将方法声明为最终方法- 所以不是这样。
或者是静态的
public function getOfficeDays(): array {
方法签名显然也不是静态的——所以也不是这样。
未指定
通过逻辑推论,这一定是问题所在——而且它是唯一需要对使用 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
有一个参数用于指定显式模拟方法,因此像这样编写测试:
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));
}
}
将生成一个有效的模拟对象并允许测试运行。
推荐阅读
- sql - SQL count(distinct) 在这种情况下如何工作?
- java - 操作方法:分布式(集群)Web 服务器中的 Web 应用程序单例
- amazon-web-services - 端点组的 API Gateway AWS 资源策略
- java - 使用 Apache POI 删除 Excel 文件中的多行
- reactjs - 即使我导航到其他页面并返回,也将数据保留在应用程序中
- vue.js - vue.config.js 中的 Vue devServer.proxy 不起作用
- r - 不允许重复的“row.names” - R
- javascript - 如何在 JavaScript 中更改 image.src?
- apache-spark - 当 Java 中的列值匹配时,通过考虑一个数据集来加入 spark 数据集
- c - 不知何故,一旦我的 cygwin gcc 编译器出现故障