Level 58
The Laracasts forum post is correct in stating that the first parameter of assertJson is an array. The documentation is outdated and needs to be updated. The correct usage of assertJson is to pass an array as the first parameter, which will be converted to JSON and compared to the response. Here is an updated example of the test code:
/** @test */
function it_user_is_authorized_can_show_role()
{
$role1 = Role::find(1);
$user = User::factory()->create([
'name' => 'Not authorized'
]);
Bouncer::allow($user)->to('full-roles');
$this->actingAs($user);
$response = $this
->withHeaders(['Accept' => 'application/json'])
->get('api/v2/roles/1') // Can return any role
->assertOk();
$this->assertJson(['data' => $role1->toArray()]);
}
Note that the $role1 variable needs to be converted to an array using the toArray() method before passing it to assertJson.
1 like