害虫php使用抽象功能的特征
我是害虫PHP的新手,并尝试在害虫PHP文件中使用特质。在一个特征中,我有一种抽象方法,必须输入路由名称。但是我不知道如何在害虫中正确调用抽象功能?
特征
<?php
namespace Tests\Traits;
use Illuminate\Support\Facades\Route;
trait UserScopeEndpointTest
{
/** @test */
public function ep_contains_auth_middleware()
{
$this->assertTrue(in_array('auth',
Route::getRoutes()->getByName($this->routeName())->gatherMiddleware()));
}
/** @test */
public function ep_contains_apply_user_scopes_middleware()
{
$this->assertTrue(in_array('applyUserScopes',
Route::getRoutes()->getByName($this->routeName())->gatherMiddleware()));
}
private abstract function routeName();
}
这是这是害虫文件的
<?php
use App\Models\Task;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\Traits\UserScopeEndpointTest;
use function Pest\Laravel\actingAs;
use function Pest\Laravel\get;
uses(RefreshDatabase::class);
uses(UserScopeEndpointTest::class);
beforeEach(function () {
$task = Task::factory()->create();
Task::factory()->create();
actingAs($task->user);
});
test('user can see all his tasks', function () {
get(route(routeName()))->assertStatus(200);
});
function routeName() {
return 'tasks.index';
}
,这是我遇到的错误
Class P\Tests\Feature\TaskIndexTest contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (P\Tests\Feature\TaskIndexTest::routeName)
at vendor/pestphp/pest/src/Factories/TestCaseFactory.php:222
218▕
219▕ final class $className extends $baseClass implements $hasPrintableTestCaseClassFQN {
220▕ $traitsCode
221▕
➜ 222▕ private static \$__filename = '$filename';
223▕ }
224▕ ");
225▕ } catch (ParseError $caught) {
226▕ throw new RuntimeException(sprintf('Unable to create test case for test file at %s', $filename), 1, $caught);
+1 vendor frames
2 [internal]:0
Whoops\Run::handleShutdown()
,有人可以告诉我如何在PEST PHP中正确使用该特质吗?
I`m new to pest PHP and trying to use a trait within an pest PHP file. Within an trait I have an abstract method where I have to enter the route name. But I dont know how to call the abstract function correctly within pest?
This is the Trait
<?php
namespace Tests\Traits;
use Illuminate\Support\Facades\Route;
trait UserScopeEndpointTest
{
/** @test */
public function ep_contains_auth_middleware()
{
$this->assertTrue(in_array('auth',
Route::getRoutes()->getByName($this->routeName())->gatherMiddleware()));
}
/** @test */
public function ep_contains_apply_user_scopes_middleware()
{
$this->assertTrue(in_array('applyUserScopes',
Route::getRoutes()->getByName($this->routeName())->gatherMiddleware()));
}
private abstract function routeName();
}
This is the Pest file
<?php
use App\Models\Task;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\Traits\UserScopeEndpointTest;
use function Pest\Laravel\actingAs;
use function Pest\Laravel\get;
uses(RefreshDatabase::class);
uses(UserScopeEndpointTest::class);
beforeEach(function () {
$task = Task::factory()->create();
Task::factory()->create();
actingAs($task->user);
});
test('user can see all his tasks', function () {
get(route(routeName()))->assertStatus(200);
});
function routeName() {
return 'tasks.index';
}
This is the error I'm getting
Class P\Tests\Feature\TaskIndexTest contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (P\Tests\Feature\TaskIndexTest::routeName)
at vendor/pestphp/pest/src/Factories/TestCaseFactory.php:222
218▕
219▕ final class $className extends $baseClass implements $hasPrintableTestCaseClassFQN {
220▕ $traitsCode
221▕
➜ 222▕ private static \$__filename = '$filename';
223▕ }
224▕ ");
225▕ } catch (ParseError $caught) {
226▕ throw new RuntimeException(sprintf('Unable to create test case for test file at %s', $filename), 1, $caught);
+1 vendor frames
2 [internal]:0
Whoops\Run::handleShutdown()
Can someone tell me how to use the trait correctly within pest php?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论