So this user is allowed to create new users?
May 26, 2020
3
Level 1
Laravel JSON API Testing
I'm looking for tips or advice on testing a JSON API. This is an internal api, not for public consuption (if that matters). The below is an example of the current format for a simple users endpoint. This works well, and is fast enough, but I'm wondering if there are better or more comprehensive methods?
/** @test */
public function it_can_store_users()
{
// Given that you are logged in.
$this->be($you = factory(User::class)->create());
// And you have permission to store the resource.
$you->givePermissionTo(['store users']);
// And you have data.
$user = factory(User::class)->make()->getAttributes();
// When you post the user data.
$response = $this->post(route('api.users.store'), $user);
// The response is successful.
$response->assertSuccessful();
// The response is data wrapped.
$response->assertJsonStructure(['data' => []]);
// The data is present in the response.
$response->assertJsonFragment($user);
// The data is present in the database.
$this->assertDatabaseHas('users', $user);
}
Please or to participate in this conversation.