[英]Laravel: Unit testing with authentication
I'm trying to put some tests in my Laravel app. 我正在尝试在Laravel应用中进行一些测试。 First, I'm checking that the login is working fine :
首先,我正在检查登录是否正常:
public function testLogin(){
$this->visit('/auth/login')
->type('mylogin', 'login')
->type('mypassword', 'password')
->press('Login')
->seePageIs('/home');
}
Ok, login is working fine! 好的,登录正常! Now, I would like to check that all the information in the page
/accountInfo
are correct: 现在,我想检查
/accountInfo
页中的所有信息是否正确:
public function testAccountInfoDisplay(){
$this->visit('/accountInfo')
->see('criticaldata');
}
But I never see /accountInfo
page, since I'm redirected because I'm not logged in. 但是我从未看到
/accountInfo
页面,因为由于我未登录而被重定向。
I've seen a few solutions in the doc like: 我在文档中看到了一些解决方案,例如:
$this->actingAs($user)
->withSession(['foo' => 'bar'])
->visit('/')
->see('Hello, '.$user->name);
But I cannot fake a session, since when I log in, I'm in fact asking for an access token to another server for the authentication. 但是我无法伪造会话,因为登录时实际上是在要求访问另一台服务器的访问令牌以进行身份验证。 Without this token, I'm unable to display any page, since the data server will refuse the connection.
没有此令牌,我将无法显示任何页面,因为数据服务器将拒绝连接。
In a nutshell, I've got this token in the testLogin
function, but it disappears after. 简而言之,我已经在
testLogin
函数中获得了该令牌,但是此令牌在之后消失了。
I can of course do the login for each test, but that's a lot of requests if I have like 150 tests to run. 我当然可以为每个测试进行登录,但是如果我要运行150个测试,那将是很多请求。
Is there a better way to keep this token during all the tests? 有没有更好的方法可以在所有测试中保留此令牌?
Here is what I did: 这是我所做的:
public function testLogin(){
$this->visit('/auth/login')
->type('myLogin', 'login')
->type('myPassowrd', 'password')
->press('Login')
->seePageIs('/home');
return Session::all();
}
/**
* @depends testLogin
*/
public function testAccountInfoDisplay($session){
foreach($session as $key=>$value){
Session::set($key, $value);
}
$this->visit('/accountInfo')
->see('4631');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.