noleafclover614@gmail.com's avatar

PHPUnit and a view composer

I'm following along with the Let's build A Forum with Laravel series, and I'm at the point where we are creating a profile for a User: https://www.laracasts.com/series/lets-build-a-forum-with-laravel/episodes/22

My problem is that I have a view composer in my AppServiceProvider class that shares a $currentUser variable with all views:

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {

        view()->composer('*', function ($view) {
            return $view->with('currentUser', auth()->user());
        });
    }

//
}

So now when I run my tests, I get an error stating that the $currentUser variable is not defined. How can I make sure my tests are picking up the shared variable?

    /** @test */
    function a_user_has_a_profile()
    {
        $user = create('App\User');
        $this->signIn($user);
        $this->get("/profiles/{$user->name}")
            ->assertSee($user->name); //throws error
    }
0 likes
1 reply
noleafclover614@gmail.com's avatar
Level 3

Alright...I'm pretty sure the error I was getting trying to get property of non-object was also being caused because I forgot to eager load a relationship I was also trying to use. For now I'll mark this as solved...

Please or to participate in this conversation.