Skip to content
Advertisement

How to assert paginations in Laravel?

I have a category model with the following method:

JavaScript

My category controller:

JavaScript

This is what I’ve tried, I am using RefreshDatabase trait.

JavaScript

This test fails for some reason:

JavaScript

Advertisement

Answer

The reason you get this error is because somehow the posts are eager loaded from the view/controller but not from the tests.

I’m guessing return self::has('posts')->with('posts')->paginate(1); could fix it.

Alternatively, you can test if you have the pagination at the bottom the page. Since {{ $categories->links() }} will add something like Previous and Next you can still look for it.

JavaScript

Also, you can ensure that you paginate the categories but it won’t ensure you have added the links at the bottom of the page.

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement