我正在使用Laravel 5.6,我有一个简单的测试来检查用户是否可以注册,如下所示;
public function it_registers_successfully()
{
$user = factory(User::class)->create([ 'password' => bcrypt('foobar') ]);
$payload = [ 'name' => $user->name, 'email' => $user->email, 'password' => 'foobar' ];
$response = $this->post('/api/register', $payload);
$response->assertStatus(200);
}
运行测试时,我收到以下信息;
Expected status code 200 but received 302.
Failed asserting that false is true.
我是测试的新手,但我认为问题在于寄存器功能会检查用户是否应该登录成功的寄存器,然后再进行操作,从而进行重定向。
我知道这无法达到目的,并且倒退了它的实现方式,但是我希望获得一些有关如何处理重定向并查看200状态的建议。
功能如下;
public $loginAfterSignUp = true;
public function register(RegisterAuthRequest $request)
{
// Build new user collection
$user = new User();
$user->name = $request->name;
$user->email = $request->email;
$user->password = bcrypt($request->password);
// Save user
$user->save();
// Check if user should be automatically logged in
if ($this->loginAfterSignUp) {
return $this->login($request);
}
return response()->json([
'success' => true,
'data' => $user
], 200);
}