Laravel测试注册重定向

时间:2017-08-08 15:25:30

标签: php laravel laravel-5 laravel-5.4

Laravel 5.4.31

我通过运行php artisan make:authphp artisan migrate来使用auth脚手架。

我在RegisterController

中覆盖了注册函数,如下所示
public function register(Request $request)
{
    $this->validator($request->all())->validate();

    event(new Registered($user = $this->create($request->all())));

    return redirect('/login')->with('message', Config::get('constants.registration.success'));
}

我正在编写如下测试。

public function a_user_is_redirected_to_login_page_with_message_after_registration() {

    $user = factory( 'App\User' )->raw();

    $response = $this->post( '/register', $user );

    $response = $this->followRedirects($response);

    $response
        ->assertSee( Config::get( 'constants.registration.success' ) );
}

我已在父followRedirects课程中定义了TestCase功能,并且我已正确设置配置constants.registration.success

但我收到以下断言错误。

PHPUnit 5.7.21 by Sebastian Bergmann and contributors.

F                                                                   1 / 1 (100%)

Time: 5.14 seconds, Memory: 20.00MB

There was 1 failure:

1) Tests\Feature\UsersTest::a_user_is_redirected_to_login_page_with_message_after_registration
Failed asserting that '<!DOCTYPE html>
the whole html content goes here
</html>
' contains "You have been registered. But your account has to be activated by the administrator before you can login. Please contact the administrator.".

C:\Users\Acer\Documents\codehub\laravel\dashbox\vendor\laravel\framework\src\Illuminate\Foundation\Testing\TestResponse.php:197
C:\Users\Acer\Documents\codehub\laravel\dashbox\tests\Feature\UsersTest.php:43

我的浏览器上也没有显示消息会话变量。可能有什么不对?

更新 我的观察是:在POST /register路线成功注册后,由于某些我不知道的原因,它首先重定向到/,然后在测试期间重定向到login。当我从$this->validator($request->all())->validate();中的register函数中移除RegisterController来电时似乎工作正常。

0 个答案:

没有答案