0
我試圖登錄,我的Laravel測試用下面的代碼用戶:Laravel 5.4測試 - 無法登錄
class AdminTest extends TestCase
{
use DatabaseMigrations;
public function setUp()
{
parent::setUp();
$this->admin = factory(User::class)->create([
'is_admin' => true,
]);
}
public function test_admin_can_create_clients()
{
$this->startSession()
->actingAs($this->admin, 'web')
->get('/clients')
->assertStatus(200);
}
}
但是我總是得到一個失敗:
1) Tests\Feature\AdminTest::test_admin_can_create_clients
Expected status code 200 but received 403. Failed asserting that false is true.
當我做了一些Google搜索時,我發現我必須startSession
,但即使這樣也行不通。
我嘗試使用be
但得到了以下錯誤:
1) Tests\Feature\AdminTest::test_admin_can_create_clients
Error: Call to a member function get() on null
我如何可以登錄用戶通過我的測試,並將它堅持在同一個測試多個頁面?
謝謝
用'be'冒充用戶 – Thamilan
已經嘗試過了。查看更新。 @Thamilan – Shiv
我不知道它是否有很大用處,但在我自己的應用程序中,我使用'$ this-> actingAs($ user) - > get(...);',因此刪除'startSession'並刪除'/ web /'參數。 – milo526