I want to test a view composer and I don't get how to...
I have a simple View Composer like this:
class SidebarComposer
{
/**
* @var Post
*/
protected $post;
public function __construct(Post $post)
{
$this->post = $post;
}
public function compose(View $view)
{
$view->with('latestPosts', $this->post->orderBy('created_at', 'desc')->take(5)->get());
}
}
And my test looks like that
class SidebarComposerTest extends TestCase
{
use DatabaseMigrations;
/** @test */
public function it_passes_the_latest_posts_to_the_view()
{
$posts = factory(Post::class,5)->create();
$composer = app(SidebarComposer::class);
$view = \Mockery::mock(View::class);
$view->shouldReceive('with')->with(['latestPosts', $posts])->once();
$composer->compose($view);
}
}
The phpunit error I get is this :
Mockery\Exception\NoMatchingExpectationException : No matching handler found for Mockery_0_Illuminate_View_View::with('latestPosts', object(Illuminate\Database\Eloquent\Collection)). Either the method was unexpected or its arguments matched no expected argument list for this method
Does anyone have any suggestions on how to test the ViewComposer correctly?