当然,可以编写一个应用程序测试,手动访问登录页面,然后填写表单并提交,但这不是必需的。相反,最简单的选项是使用->be()方法模拟以用户身份登录。请看示例9-30
Example 9-30. Authenticating as a user in application tests
public function test_it_creates_a_new_contact() {
$user = factory(User::class)->create();
$this->be($user);
$this->post('contacts', [
'email' => 'my@email.com',
]);
$this->assertDatabaseHas('contacts', [
'email' => 'my@email.com',
'user_id' => $user->id,
]);
}
public function test_it_creates_a_new_contact() {
$user = factory(User::class)->create();
$this->actingAs($user)->post('contacts', [
'email' => 'my@email.com',
]);
$this->assertDatabaseHas('contacts', [
'email' => 'my@email.com',
'user_id' => $user->id,
]);
}
Example 9-31. Testing authorization rules
public function test_non_admins_cant_create_users() {
$user = factory(User::class)->create([
'admin' => false,
]);
$this->be($user);
$this->post('users', ['email' => 'my@email.com']);
$this->assertDatabaseMissing('users', [
'email' => 'my@email.com',
]);
}
Example 9-32. Testing authorization rules by checking status code
public function test_non_admins_cant_create_users() {
$user = factory(User::class)->create([
'admin' => false,
]);
$this->be($user);
$response = $this->post('users', ['email' => 'my@email.com']);
$response->assertStatus(403);
}
Example 9-33. Testing authentication routes
public function test_users_can_register() {
$this->post('register', [
'name' => 'Sal Leibowitz',
'email' => 'sal@leibs.net',
'password' => 'abcdefg123',
'password_confirmation' => 'abcdefg123',
]);
$this->assertDatabaseHas('users', [
'name' => 'Sal Leibowitz',
'email' => 'sal@leibs.net',
]);
}
public function test_users_can_log_in() {
$user = factory(User::class)->create([
'password' => Hash::make('abcdefg123')
]);
$this->post('login', [
'email' => $user->email,
'password' => 'abcdefg123',
]);
$this->assertTrue(auth()->check());
$this->assertTrue($user->is(auth()->user()));
}
与5.4之前版本测试方法名的不同
在运行5.4之前版本的laravel的项目中,assertDatabaseHas()应该被替换成seeInDatabase(),assertDatabaseMissing()应该被替换成dontSeeInDatabase(),assertDatabaseHas()应被替换成seeInDatabase(),应该在$ this而不是$ response上调用assertStatus()