0
votes

While writing my tests for API the route api/users should return a list of users. In Postman works as it should but in my phpunit test returns status 200 but an empty response hence assertJsonStructure() fails.

I have used the same user/password for Postman and for phpUnit. I added dd() trying to debug the response and get an empty body.

The test

/** @test */                                                                                                                                                  
         public function testUserCanListUsers()                                                                                                                        
         {                                                                                                                                                             
         ┆   $user = factory(User::class)->make();                                                                                                                     
         ┆   \Auth::loginUsingId($user);                                                                                                                               
         ┆   $response = $this->actingAs($user)                                                                                                                        
         ┆   ┆   ->get('api/users')                                                                                                                                    
         ┆   ┆   ->assertStatus(200);                                                                                                                                  
         |   dd($response->getContent());                                                                                                                              
         ┆   $response->assertJsonStructure([                                                                                                                          
         ┆   ┆   "id",                                                                                                                                                 
         ┆   ┆   "name",                                                                                                                                               
         ┆   ┆   "email",                                                                                                                                              
         ┆   ┆   "email_verified_at",                                                                                                                                  
         ┆   ┆   "created_at",                                                                                                                                         
         ┆   ┆   "updated_at",                                                                                                                                         
         ┆   ]);                                                                                                                                                       
         }     

The expected output which I get on Postman

[
    {
        "id": 1,
        "name": "Dr. Frederic Klein",
        "email": "[email protected]",
        "email_verified_at": "2018-10-31 20:03:52",
        "created_at": "2018-10-31 20:03:52",
        "updated_at": "2018-10-31 20:03:52"
    },
    {
        "id": 2,
        "name": "Prof. Alyce Considine",
        "email": "[email protected]",
        "email_verified_at": "2018-10-31 20:03:52",
        "created_at": "2018-10-31 20:03:52",
        "updated_at": "2018-10-31 20:03:52"
    },
    {
        "id": 3,
        "name": "Miss Jermaine Johns PhD",
        "email": "[email protected]",
        "email_verified_at": "2018-10-31 20:03:52",
        "created_at": "2018-10-31 20:03:52",
        "updated_at": "2018-10-31 20:03:52"
    },...

The output I get on phpUnit When I dd($response); "[]"

Postman output

1

1 Answers

1
votes

The factory()->make() method does not save a record in the database. Therefore, when you make your api call in your test, you don't have any users, hence the empty response.

factory()->create() is used to actually save the record. So, just change your factory()->make() to factory()->create() and you should get a user in your response.