jewishmoses
1 month ago
174
1
Testing

How to assert paginations in Laravel?

Posted 1 month ago by jewishmoses

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

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