mercredi 11 juillet 2018

How to test using phpunit testing for asserting data returned from a query in Laravel?

I want to test that when a user logs in and goes to my_posts then he should see all posts created by him.

I have done this so far:

public function test_user_should_see_own_posts()
{
   $user = User::all()->random();

   $response = $this->actingAs($user)
                         ->get('/my_posts');

   // get all posts of the user
   $posts = DB::table('posts')
                ->join('users', 'posts.user_id', 'users.id')
                ->select('title', 'body')
                ->where('users.id', $user->id)
                ->groupBy('posts.id')
                ->latest('posts.created_at')
                ->get();

   // I want to assert that the posts are displayed here of the user
}

So now how should I assert that the posts of the user are displayed on this page ?

Also please provide any good resources for learning phpunit testing as I cannot find any good resources.

Most of my testing is related to whether the query returns correct output to the view. So it is mostly CRUD based testing.

Aucun commentaire:

Enregistrer un commentaire