Dealing with redirections in testing environment

Posted 5 months ago by Chris1981

I am using Laravel 5.6 with the JWT-Auth package, I have a simple test which checks a user is able to be registered, as follows;

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);

When i run the test i am receiving the following;

Expected status code 200 but received 302.
Failed asserting that false is true.

I am new to testing, but i think the problem is that the register function checks to see if the user should be logged in on a successful register and then does it, hence the redirection.

I know this is defeating the purpose and going backwards to how its meant to be done, but i would like some advice on how to deal with the redirection and see the 200 status.

The function is as follows;

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

    // Check if user should be automatically logged in
    if ($this->loginAfterSignUp) {
        return $this->login($request);

    return response()->json([
        'success' => true,
        'data' => $user
    ], 200);

Please sign in or create an account to participate in this conversation.

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up


Reply to

Use Markdown with GitHub-flavored code blocks.