【发布时间】:2019-12-26 04:23:45
【问题描述】:
我曾经使用 PHPUnit 的方法设置来为我的测试方法创建一个实例。但是在 Laravel 5.8 中我做不到
我已经尝试了这两种方法,并且它的工作原理为每个方法创建一个实例,如下所示。
这行得通:
<?php
namespace Tests\Unit;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
use App\Service\MyService;
class MyServiceTest extends TestCase
{
/**
* A basic unit test example.
*
* @return void
*/
public function testInstanceOf()
{
$myService = new MyService;
$this->assertInstanceOf( 'App\Service\MyService' , $myService );
}
}
这不起作用:
<?php
namespace Tests\Unit;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
use App\Service\MyService;
class MyServiceTest extends TestCase
{
private $instance;
function setUp(){
$this->instance = new MyService;
}
/**
* A basic unit test example.
*
* @return void
*/
public function testInstanceOf()
{
$myService = $this->instance;
$this->assertInstanceOf( 'App\Service\MyService' , $myService );
}
}
以下错误消息显示在控制台中:
PHP Fatal error: Declaration of Tests\Unit\MyServiceTest::setUp() must be compatible with Illuminate\Foundation\Testing\TestCase::setUp(): void in /home/myproject/tests/Unit/MyServiceTest.php on line 10
【问题讨论】:
标签: laravel unit-testing phpunit