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);
}
}
getMock()
no longer exists in PHPUnit 6. Use createMock()
or getMockBuilder()
instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With