dow98764
2017-03-13 11:06
浏览 133
已采纳

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

Update: with $stub = $this->createMock('Config'); this example works, but I get a warning:

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

In the video-tutorial this example works without any warnings. Is it possible to fix this warning?


I can't find why I am getting this error and how to fix it. This code is from a video tutorial. And in the Video it works. Maybe a typo?

Error:

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.

Here my code:

Config.php

<?php

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

DateFormatter.php

class 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);
    }
}

图片转代码服务由CSDN问答提供 功能建议

更新: $ stub = $ this-&gt; createMock(' 配置'); 此示例有效,但我收到警告:

确定,但不完整,跳过或有风险的测试! 测试:1,断言:0, 风险:1。

在视频教程中,此示例无需任何警告即可运行。 是否可以修复此警告?


我找不到为什么会收到此错误以及如何解决此问题。 此代码来自视频教程。 在视频中它可以工作。 可能是拼写错误?

错误:

c:\ laragon \ www \ phpunitλphpunit -colors tests \ DateFormatterTest.php 由Sebastian Bergmann和贡献者提供的PHPUnit 6.0.0。

E 1 / 1(100%) < p>时间:35毫秒,内存:4.00MB

有1个错误:

1)DateFormatterTest :: testFormattingDatesBasedOnConfig错误:调用 undefined方法DateFormatterTest :: getMock()

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

错误! 测试:1,断言:0,错误:1。

这里我的代码:

Config.php

 &lt;?php 
 
class Config {
 public function get(){
 return'dm-Y'; 
} 
  } 
   
 
 

DateFormatter.php

class DateFormatter { protected $ config;

  public function __construct(Config $ config){
 $ this-&gt; config = $ config; 
} 
 
公共函数getFormattedDate($ timestamp){\  n返回日期($ this-&gt; config-&gt; get('date.format'),$ timestamp); 
} 
   
 
 

}

DateFormatterTest.php

 &lt;?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扩展TestCase {
公共函数testFormattingDatesBasedOnConfig(  ){
 $ stub = $ this-&gt; getMock('Config'); 
 
 var_dump($ stub); 
} 
  } 
   
 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dshtze500055 2017-03-13 11:08
    已采纳

    getMock() no longer exists in PHPUnit 6. Use createMock() or getMockBuilder() instead.

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题