PWParsons
5 days ago
362
4
Testing

How to test a view composer

Posted 5 days ago by PWParsons

I'm new to testing so please forgive me if I am missing something obvious.

I'm trying to test the following view composer:

namespace App\Http\View\Composers;

use App\Models\Category;
use Illuminate\View\View;

class CategoryComposer
{
    public function compose(View $view)
    {
        $view->with(
            'categories',
            Category::getParents()
        );
    }
}

The test:

class CategoryComposerTest extends TestCase
{
    /** @test **/
    public function it_passes_the_parent_categories_to_the_view(): void
    {
        $composer = new CategoryComposer;

        $view = $this->mock(View::class);

        $view->shouldReceive(['with'])
            ->with([
                'categories',
                Category::getParents()
            ])
            ->once();

        $composer->compose($view);
    }
}

The following error occurs when running the test:

Mockery\Exception\BadMethodCallException : Received Mockery_2_Illuminate_View_View::with(), but no expectations were specified

What am I doing wrong?

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