2
votes

I'm writing some test cases for my Laravel app with PHPUnit.

Below is the class that is producing the error:

<?php

namespace Test\Feature;

use Tests\TestCase;
use App\Models\User;
use Illuminate\Foundation\Testing\DatabaseMigrations;

class UserLoginTest extends TestCase
{
    use DatabaseMigrations;

    public function setUp()
    {
        parent::setUp();
        $this->admin = factory(User::class)->create([
                'is_admin'  => true,
                'password'  => bcrypt('secret'),
            ]);
    }

    /** @test */
    public function test_login_user_form()
    {
        $this->get('/login')
            ->assertStatus(200);
    }

    /** @test */
    public function test_login_user_form_submission()
    {
        $this->post('/login', [
                'email'     => $this->admin->email,
                'password'  => 'secret',
            ]);

        $this->assertRedirectedTo('/');
    }
}

The problem is that when I run PHPUnit I get this error:

PHPUnit 5.7.20 by Sebastian Bergmann and contributors. ....E.. 7 / 7 (100%) Time: 1.65 seconds, Memory: 20.00MB There was 1 error: 1) Test\Feature\UserLoginTest::test_login_user_form_submission Error: Call to undefined method Test\Feature\UserLoginTest::assertRedirectedTo() /Users/shivampaw/Desktop/valet/UltimateCRM/tests/Feature/UserLoginTest.php:37

It says that assertRedirectedTo is an undefined method but I do not have any clue why. I have tried methods like assertRedirect but cannot get this working!

1

1 Answers

5
votes

You need to call assertRedirect and not assertRedirectedTo. Also all assertions need to be called on the response object

/** @test */
public function test_login_user_form_submission()
{
    $this->post('/login', [
        'email'     => $this->admin->email,
        'password'  => 'secret',
    ])->assertRedirect('/'); // <-- Chain it to the response
}