laravel黄昏tearDown()必须与Illuminate \ Foundation \ Testing \ TestCase :: tearDown()兼容 [英] laravel dusk tearDown() must be compatible with Illuminate\Foundation\Testing\TestCase::tearDown()
问题描述
public function tearDown()
{
$this->browse(function (Browser $browser) {
$browser->click('#navbarDropdown')
->click('.dropdown-item');
});
parent::tearDown();
}
在我的测试类上应用tearDown()方法时,出现一条错误消息,告诉我the tearDown() must be compatible with Illuminate\Foundation\Testing\TestCase::tearDown()
我在做什么错了?
When I apply the tearDown() method to my test class I get an error telling me the tearDown() must be compatible with Illuminate\Foundation\Testing\TestCase::tearDown()
What am I doing wrong?
每次运行测试时,我都需要登录.我想登录setUp()方法,然后再次在tearDown中注销,所以我可以独立执行测试.
Every time I run a test I need to login. I want to login in the setUp() method and the log out again in the tearDown, so I can execute my tests independently.
这是我的setUp()方法
This is my setUp() method
use databaseMigrations;
public function setUp(): void
{
parent::setUp();
$this->seed('DatabaseSeeder');
$this->browse(function (Browser $browser) {
$browser->visit('/admin')
->type('email', 'admin@admin.com')
->type('password', 'admin')
->press('Login');
});
}
setUp()方法可以正常工作.即使我没有向parent::tearDown();
以外的任何代码添加到tearDown()方法中,也会出现错误,那么我的tearDown()方法在做什么错了?
The setUp() method works just fine. Even when I don't add any code to the tearDown() method, except the parent::tearDown();
, I get an error, so what am I doing wrong in my tearDown() method?
public function tearDown()
{
parent::tearDown();
}
推荐答案
您缺少tearDown()
上的: void
:
public function tearDown(): void
{
parent::tearDown();
}
您的setUp()
正确,但是作为父类方法的这两种方法都需要兼容,而省略: void
则不兼容.
You have setUp()
correct, but both methods, as method of the parent class, need to be compatible and omitting the : void
isn't.
每当您看到该错误时,最好检查一下您要扩展的类的来源.通过继承,即
Any time you see that error, it's best to check the source of the class you're extending. By inheritance, that is
Illuminate \ Foundation \ Testing \ TestCase.php
Illuminate\Foundation\Testing\TestCase.php
/**
* Setup the test environment.
*
* @return void
*/
protected function setUp(): void
{
...
}
...
/**
* Clean up the testing environment before the next test.
*
* @return void
*/
protected function tearDown(): void
{
...
}
这篇关于laravel黄昏tearDown()必须与Illuminate \ Foundation \ Testing \ TestCase :: tearDown()兼容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!