I'm new in unit tests in laravel and currently facing an error on my test. Please see my code below.
Test
/** @test */
public function users_can_view_homepage_products()
{
$response = $this->get('api/products');
$response->assertStatus(200)
->assertJson([
'id' => 1,
'name' => ucwords($this->faker->words(3, true)),
'slug' => ucfirst($this->faker->slug),
'intro' => $this->faker->sentence,
'price' => number_format($this->faker->randomFloat(2, 100, 99999), 2)
]);
}
Controller
public function index()
{
return [
'id' => 1,
'name' => 'Airpods Pro (2021)',
'slug' => 'airpods-pro-2021',
'intro' => 'New and powerful airpods from apple.',
'price' => 12400
];
}
Error
CodePudding user response:
The test fails because the content of the json is different to that being tested. You probably want to test that the structure is the same, rather than the content:
/** @test */
public function users_can_view_homepage_products()
{
$response = $this->get('api/products');
$response->assertStatus(200)
->assertJsonStructure([
'id',
'name'
'slug'
'intro'
'price'
]);
}