3 years ago

Problem with assertRedirectedToRoute()

Posted 3 years ago by smnhunt


I'm probably making a daft mistake but just don't seem to be able to get assertRedirectToRoute() to work in my tests - any help would be gratefully appreciated.

I have a controller function which returns a view with a form:

    public function create(School $school)
        return view('form.create-teacher', [
            'school' => $school,

And the form action direct to this function:

    public function store(Request $request, School $school)
        $this->validate($request, [
            'name' => 'required|max:255',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|min:6|confirmed',

        $user = User::create([
            'name' => $request->name,
            'email' => $request->email,
            'password' => $request->password,

        $teacher = Teacher::create();

        return \Redirect::route('school.{school}.teacher.index', ['school' => $school->id]);

The test that I am running is as follows:

    public function a_school_admin_can_add_a_teacher_to_their_school()
        // Given: A school exists and I visit the create teacher page.
        $school = factory(App\Base\Academy\School::class, 1)->create();

        // When: The teacher is registered.
        $this->type('Mr Smith', 'name')
             ->type('[email protected]', 'email')
             ->type('password', 'password')
             ->type('password', 'password_confirmation')

        // Then: The changes are reflected in the database.
        $this->seeInDatabase('users', ['name' => 'Mr Smith']);
        $this->seeInDatabase('teachers', ['user_id' => 1, 'school_id' => 1]);
        $this->assertRedirectedToRoute('school.{school}.teacher.index', ['school' => 1]);

And the error that I am receiving is as follows:

PHPUnit 4.8.27 by Sebastian Bergmann and contributors.


Time: 179 ms, Memory: 16.00MB

There was 1 failure:

1) SchoolAdminTest::a_school_admin_can_add_a_teacher_to_their_school
Failed asserting that Illuminate\Http\Response Object (...) is an instance of class "Illuminate\Http\RedirectResponse".


Tests: 2, Assertions: 10, Failures: 1.

It seems as though all other parts of the test are working except for the assertRedirectedToRoute part.

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