Dealing with redirections in testing environment

Published 1 week 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);
    $response->assertStatus(200);
}

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
    $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);
}
lostdreamer_nl

There is either a 200 status, or a 302 redirect, so you'll need to test both situations. I would move te $loginAfterSignup boolean to the config files (where configuration belongs) so the tests can also read them.

after that it's just a matter of testing if you got back a redirect response:

$response->assertRedirect($uri); // change uri to the uri the user would be redirected to after successfull login

or the json response with a 200

Chris1981

@lostdreamer_nl thanks for that ... But how would I change the value of loginAfterSignUp in a test if i move it to config?

Cronix
Cronix
1 week ago (566,300 XP)

You can temporarily set config values, too. It won't persist and will only be good for the lifecycle of the request that it was set in. https://laravel.com/docs/5.6/configuration#accessing-configuration-values

So have it set in config for normal use, and set it to something else during runtime when you're testing before trying to access it.

Chris1981

Hey Guys,

Thanks for the answers, but how do i change the property value in the test?

If I do something like;

public function it_registers_successfully()
{
    $loginAfterSignUp = false;

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

I still get the same result

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