How to assert paginations in Laravel?
I have a category model with the following method:
public static function index()
{
return self::has('posts')->paginate(1);
}
My category controller:
public function index()
{
$categories = Category::index();
return view('categories.index', compact('categories'));
}
This is what I've tried:
$response = $this->get(route('categories.index'));
$response->assertViewHas('categories', Category::index());
This test fails for some reason:
Failed asserting that two objects are equal.
at tests/Feature/CategoryTest.php:38
37| $response->assertViewIs('categories.index');
> 38| $response->assertViewHas('categories', Category::index());
--- Expected
+++ Actual
@@ @@
'dispatchesEvents' => Array ()
'observables' => Array ()
'relations' => Array (
+ 'posts' => Illuminate\Database\Eloquent\Collection Object (...)
)
'touches' => Array ()
'timestamps' => true
I would create 2 categories, and then just make sure that you can only see the first one (because the pagination):
$category1 = factory(Category::class)->create();
$category2 = factory(Category::class)->create();
$response = $this->get(route('categories.index'));
$response
->assertSee($category1->name)
->assertDontSee($category2->name);
Just updated the example with categories.
Please or to participate in this conversation.