ignium's avatar
Level 21

Single test fails with BindingResolutionException after migrating to Laravel 8.x

I recently upgraded (with Laravel Shift) to Laravel 8.x and after tweaking a few of my tests, my entire test suite passes except for a single test with the following error:

There was 1 error:

  1. Tests\Feature\UserProfileVerificationFeatureTest::an_admin_profile_will_be_verified_once_edited Illuminate\Contracts\Container\BindingResolutionException: Target class [web] does not exist.

[stack trace removed for brevity]

Caused by ReflectionException: Class web does not exist

[stack trace removed for brevity]

Failing Test

    /** @test */
    public function an_admin_profile_will_be_verified_once_edited()
    {
        $admin = $this->signIn(['admin', 'profile_not_verified']);
        // $admin = User::factory()->admin()->profileNotVerified()->create();
        // $this->actingAs($admin);

        $this->assertFalse($admin->profileIsVerified());

        $unverified_text = __('app.Profile Not Verified');

        $this->get(route('home'))    //<=== FAILS HERE
            ->assertOk()
            ->assertSeeText($unverified_text);

        Livewire::test(StaffAdminForm::class, ['staff' => $admin])
            ->call('submit')
            ->assertHasNoErrors()
            ->assertRedirect(route('staff.show', $admin))
            ->assertSessionHas('flash', $this->getSelfUpdateMessage());

        $admin = $admin->refresh();

        $this->assertTrue($admin->profileIsVerified());

        $this->get(route('home'))
            ->assertOk()
            ->assertDontSeeText($unverified_text);
    }

Passing Test (in same test class)


    /** @test */
    public function a_user_updating_their_unverified_profile_will_verify_it()
    {
        $user = $this->signIn('profile_not_verified');

        $this->assertFalse($user->profileIsVerified());

        $unverified_text = __('app.Profile Not Verified');

        $this->get(route('home'))
            ->assertOk()
            ->assertSeeText($unverified_text);

        Livewire::test(StaffForm::class, ['staff' => $user])
            ->call('submit')
            ->assertHasNoErrors()
            ->assertRedirect(route('staff.show', $user))
            ->assertSessionHas('flash', $this->getSelfUpdateMessage());

        $user = $user->refresh();

        $this->assertTrue($user->profileIsVerified());

        $this->get(route('home'))
            ->assertOk()
            ->assertDontSeeText($unverified_text);
    }

Any ideas why this is happening? The only difference is the parameters for the signIn() function, but I also tested passing an array to the passing test and it still passed, also in both cases the User is created successfully and the first $this->assertFalse($user->profileIsVerified() call passes without a problem. It's only when trying to visit the page that the BindingResolutionException is thrown.

0 likes
14 replies
neilstee's avatar

@ignium I think this is an issue on your routes, can you share your route for route('home')?

ignium's avatar
Level 21

Route $namespace property is still set in the RouteServiceProvider, and the other requests to that route work fine in the same test class. That last part is the part that's confusing me.

From RouteServiceProvider
protected $namespace = 'App\Http\Controllers';

Sidenote: the test fails, but navigating to the page works fine

ignium's avatar
Level 21

@neilstee It's the unedited default route from the initial installation (Laravel 5.4 I think). I also tried with the static class syntax, but it didn't make a difference.

Route::get('/home', 'HomeController@index')->name('home');
//Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])
//          ->name('home');
neilstee's avatar

@ignium try

Route::get('/home', 'App\Http\Controllers\HomeController@index')->name('home');
neilstee's avatar

You said you are receiving BindingResolutionException when you visit the homepage? Can you check the stack trace where it really fails in the code?

neilstee's avatar

@ignium also, this might be an issue on your PHPUnit running globally.

Instead of running phpunit use the one that installed on your vendor file ./vendor/bin/phpunit

neilstee's avatar

and lastly, if still no luck on that, run composer dump-autoload

ignium's avatar
Level 21
  • Updating to the route you suggested (but also adding a leading slash) ended in the same result
  • running the test from the vendor file also ended in the same result (I'm on Homestead and by default phpunit is an alias for ./vendor/bin/phpunit) But I also ran it directly as you suggested from the terminal
  • Stack trace ends at .../vendor/laravel/framework/src/Illuminate/Container/Container.php:835 which is the Container.build() method
ignium's avatar
Level 21

Ugh... That didn't work either. This is really baffling. I might just mark this test skipped. I have other tests in the class that check for each of the things this one tests for. This final test was just kind of make sure it all works together test. And I was hoping to understand what was causing the problem.

I appreciate your time and willingness to help!

neilstee's avatar

@ignium check your folder permission as well, you said you are using Homestead.

Hope you find the fix, share it here when you do so others with the same problem can fix it too.

ignium's avatar
Level 21

@neilstee I'm not sure it's a permission issue, because there are six other tests in that class and four of them call that route without a problem. There's also several other routes in the routes/web file and the other 1000+ tests are all passing. I feel okay with throwing my hands up on this one for now. If I figure it out I'll definitely add the solution here (and be sure to tag you).

Thanks again for you willingness to help. I've seen you on the boards quite a bit helping people out, and just wanted you to know it doesn't go unnoticed!

1 like
neilstee's avatar

@ignium hope you get it to fix soon! and thanks for the kind words, means a lot to me!

Please or to participate in this conversation.