laravel Laravel5 单元测试登录表单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34714086/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel5 Unit Testing a Login Form
提问by user3732216
I ran the following test and I am receiving a failed_asserting that false is true. Can someone further explain why this could be?
我运行了以下测试,我收到了一个 failed_asserting,表明 false 为真。有人可以进一步解释为什么会这样吗?
/** @test */
public function a_user_logs_in()
{
$user = factory(App\User::class)->create(['email' => '[email protected]', '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'));
}
回答by Denis Mysenko
Your PHPUnit test is a client, not the web application itself. Therefore Auth::check() shouldn't return true. Instead, you could check that you are on the right page after pressing the button and that you see some kind of confirmation text:
您的 PHPUnit 测试是一个客户端,而不是 Web 应用程序本身。因此 Auth::check() 不应返回 true。相反,您可以在按下按钮后检查您是否在正确的页面上,并且您会看到某种确认文本:
/** @test */
public function a_user_can_log_in()
{
$user = factory(App\User::class)->create([
'email' => '[email protected]',
'password' => bcrypt('testpass123')
]);
$this->visit(route('login'))
->type($user->email, 'email')
->type('testpass123', 'password')
->press('Login')
->see('Successfully logged in')
->onPage('/dashboard');
}
I believe this is how most developers would do it. Even if Auth::check() worked – it would only mean a session variable is created, you would still have to test that you are properly redirected to the right page, etc.
我相信这是大多数开发人员会这样做的方式。即使 Auth::check() 有效——它只意味着创建了一个会话变量,你仍然需要测试你是否正确重定向到正确的页面等。
回答by Narayana Reddy Gurrala
In your test you can use your Model to get the user, and you can use ->be($user) so that it will get Authenticate.
在您的测试中,您可以使用您的模型来获取用户,并且您可以使用 ->be($user) 以便它获得 Authenticate。
So i written in my test case for API test
所以我写在我的测试用例中进行 API 测试
$user = new User(['name' => 'peak']);
$this->be($user)
->get('/api/v1/getManufacturer')
->seeJson([
'status' => true,
]);
it works for me
这个对我有用