php - 错误:调用未定义的方法 DateFormatterTest::getMock()

标签 php unit-testing phpunit

更新: 使用 $stub = $this->createMock('Config'); 此示例有效,但我收到警告:

OK, but incomplete, skipped, or risky tests! Tests: 1, Assertions: 0, Risky: 1.

在视频教程中,此示例可以正常运行,不会出现任何警告。是否可以修复此警告?

<小时/>

我找不到为什么会收到此错误以及如何修复它。该代码来自视频教程。在视频中它有效。也许是拼写错误?

错误:

c:\laragon\www\phpunit λ phpunit --colors tests\DateFormatterTest.php PHPUnit 6.0.0 by Sebastian Bergmann and contributors.

E 1 / 1 (100%)

Time: 35 ms, Memory: 4.00MB

There was 1 error:

1) DateFormatterTest::testFormattingDatesBasedOnConfig Error: Call to undefined method DateFormatterTest::getMock()

C:\laragon\www\phpunit\tests\DateFormatterTest.php:10

ERRORS! Tests: 1, Assertions: 0, Errors: 1.

这是我的代码:

Config.php

<?php

class Config {
    public function get() {
        return 'd-m-Y';
    }
}

DateFormatter.php

类 DateFormatter { protected $config;

public function __construct (Config $config) {
    $this->config = $config;
}

public function getFormattedDate($timestamp) {
    return date($this->config->get('date.format'), $timestamp);
}

}

DateFormatterTest.php

<?php

use PHPUnit\Framework\TestCase;

require_once 'C:\laragon\www\phpunit\src\DateFormatter.php';
require_once 'C:\laragon\www\phpunit\src\Config.php';

class DateFormatterTest extends TestCase {
    public function testFormattingDatesBasedOnConfig() {
        $stub = $this->getMock('Config');

        var_dump($stub);
    }
}

最佳答案

getMock() 在 PHPUnit 6 中不再存在。请使用 createMock()getMockBuilder() 代替。

关于php - 错误:调用未定义的方法 DateFormatterTest::getMock(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42762098/

相关文章:

使用 PHP7.2 和 7.1 运行的 phpunit 测试比使用 PHP7.0 运行时慢 3 倍

php - 入门主题 Prestashop 1.7 为空白

php echo 如果查询为空

php - 无法显示从 AES_DECRYPT 检索到的特殊字符

python - 涉及数据库的单元测试

java - 如何模拟或以其他方式测试 readPassword?

php - PHP 如何取消 RabbitMQ 中的消费者?

javascript - AngularJS 单元测试 : Injected Services undefined

Laravel:队列作业如何工作?

php - 使用 phpunit 进行 Laravel 测试