A
A
asdasdqwe2022-01-18 07:15:38
Laravel
asdasdqwe, 2022-01-18 07:15:38

How to test models in laravel?

While I don’t know the tests well

There is an ordinary controller

public function index(Request $request)
    {
        $users= User::all();

        return view('index', compact('users'));
    }


How to test it? That is, how to make sure that User::all returns data, just create a mock and check if the all method was called?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
jazzus, 2022-01-18
@asdasdqwe

http tests https://laravel.com/docs/8.x/http-tests
methods assertOk, assertViewHas users, assertSee user full_name. That will cover the entire request.
If the database is not destroyed before the test, then you can get to the collection of users using the getOriginalContent() method on the response
and check the presence in the contains collection using the method

S
spaceatmoon, 2022-01-18
@spaceatmoon

Never test third party code, only test your own code.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question