我运行了以下测试,我收到一个失败的错误,即false是真的。有人可以进一步解释为什么会这样吗?
/** @test */
public function a_user_logs_in()
{
$user = factory(App\User::class)->create(['email' => 'john@example.com', 'password' => bcrypt('testpass123')]);
$this->visit(route('login'));
$this->type($user->email, 'email');
$this->type($user->password, 'password');
$this->press('Login');
$this->assertTrue(Auth::check());
$this->seePageIs(route('dashboard'));
}
答案 0 :(得分:5)
您的PHPUnit测试是客户端,而不是Web应用程序本身。因此,Auth :: check()不应返回true。相反,您可以在按下按钮后检查您是否在右页,并且您看到某种确认文本:
/** @test */
public function a_user_can_log_in()
{
$user = factory(App\User::class)->create([
'email' => 'john@example.com',
'password' => bcrypt('testpass123')
]);
$this->visit(route('login'))
->type($user->email, 'email')
->type('testpass123', 'password')
->press('Login')
->see('Successfully logged in')
->onPage('/dashboard');
}
我相信大多数开发人员都会这样做。即使Auth :: check()有效 - 它只会创建一个会话变量,你仍然需要测试你是否正确地重定向到了正确的页面等等。
答案 1 :(得分:2)
在您的测试中,您可以使用您的模型来获取用户,并且您可以使用 - > be($ user)以便它将获得身份验证。
所以我在我的测试用例中写了API测试
$user = new User(['name' => 'peak']);
$this->be($user)
->get('/api/v1/getManufacturer')
->seeJson([
'status' => true,
]);
它对我有用