我正在尝试将Pest和单元测试添加到Nova Admin/Laravel应用程序中。
我想测试一个页面是否存在,但我不想处理用户是否被认证为管理员来查看页面。我试图测试一个页面是否存在,无论用户是否登录,是否具有特定的角色,是否具有特定的权限等。
每次运行依赖于路由的测试时,页面都被重定向到登录页面。如何禁用重定向并绕过所有与角色/权限/身份验证相关的设置,以简单地测试页面的内容?
class ExampleTest extends TestCase
{
public function test_it_should_have_a_login_page()
{
$response = $this->get('/login');
$response->assertStatus(200); //<-- this passes
}
public function test_it_should_have_a_routing_resources_page()
{
$response = $this
->withoutMiddleware(\App\Http\Middleware\Authenticate::class)
->withoutMiddleware(\App\Http\Middleware\RedirectIfAuthenticated::class)
->withoutMiddleware(\App\Http\Middleware\VerifyCsrfToken::class)
->get('/resources/routing');
$response->assertStatus(200); //<-- this fails with status of 302: the page has redirected to /login
}
}
附加的,不成功的尝试:
public function test_it_should_have_a_routing_resources_page()
{
$user = User::factory()->create();
$role = NovaRole::create(['name' => 'admin', 'updated_at' => now(), 'created_at' => now(), 'slug' => 'admin']);
$user->assignRole($role->name);
$permissions = config('nova-permissions.permissions');
foreach ($permissions as $permission) {
$user->hasPermissionTo($permission);
}
$response = $this
->actingAs($user)
->withoutMiddleware(\App\Http\Middleware\Authenticate::class)
->withoutMiddleware(\App\Http\Middleware\RedirectIfAuthenticated::class)
->withoutMiddleware(\App\Http\Middleware\VerifyCsrfToken::class)
->get('/resources/routing');
$response->assertStatus(200); //<-- now this fails and returns 403, forbidden
}
1条答案
按热度按时间laximzn51#
啊哈!!我试图绕过错误的中间件。真正有助于解决这个问题的是进入
nova.php
并注解掉'api_middleware'
中的每个条目,直到我找到阻塞测试调用的中间件。工作代码如下: