So, i'm writing tests using laravel 8.8. In other previous versions, while testing i could write $this->withoutExceptionHandling()
to bubble up the exact error and know how to tackle it. i'm having issues doing the same with version 8.8.
My Code.
<?php
namespace Tests\Feature;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
use App\Models\User;
class AuthenticationTest extends TestCase
{
use RefreshDatabase;
/**
* Test if it checks for validation at the point of registarion.
*
* @return void
*/
public function testValidationDuringRegistration()
{
$this->withoutExceptionHandling();
$response = $this->post('/api/v1/register', ['Accept' => 'application/json']);
$response
->assertStatus(406)
->assertJsonValidationErrors(['first_name', 'last_name', 'email', 'password']);
}
/**
* Test if it checks for validation at the point of registarion.
*
* @return void
*/
public function testValidationIfSomeRequiredFieldsAreFilled()
{
$this->withoutExceptionHandling();
$response = $this->post('/api/v1/register', [
'Accept' => 'application/json',
'first_name' => 'test',
'last_name' => 'test_last_name'
]);
$response
->assertStatus(406)
->assertJsonMissingValidationErrors(['first_name', 'last_name']);
}
public function testIfRegistrationIsSuccessful()
{
$this->withoutExceptionHandling();
$response = $this->post('/api/v1/register', [
User::factory()->create()
]);
$response
->assertStatus(200);
}
}
My UserFactory.php
<?php
namespace Database\Factories;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
class UserFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = User::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'name' => $this->faker->name,
'lastname' => $this->faker->name,
'email' => $this->faker->unique()->safeEmail,
'email_verified_at' => now(),
'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
'remember_token' => Str::random(10),
];
}
}
Test result
FAIL Tests\Feature\AuthenticationTest
⨯ if registration is successful
---
• Tests\Feature\AuthenticationTest > if registration is successful
Expected status code 200 but received 406.
Failed asserting that 200 is identical to 406.
at tests/Feature/AuthenticationTest.php:58
54▕ User::factory()->create()
55▕ ]);
56▕
57▕ $response
➜ 58▕ ->assertStatus(200);
59▕ }
60▕ }
61▕
Tests: 1 failed
Time: 5.66s
Handler.php
/**
* Register the exception handling callbacks for the application.
*
* @return void
*/
public function register()
{
//
$this->renderable(function (\AuthenticationException $e, $request) {
// return response()->view('errors.custom', [], 500);
return response()->json([
'success' => false,
'message' => 'Unauthenticated.',
], 403);
});
$this->renderable(function (\ModelNotFoundException $e, $request) {
return response()->json([
'success' => false,
'message' => 'Resource could not be found.',
], 403);
});
$this->renderable(function (\ArgumentCountError $e, $request) {
return response()->json([
'success' => false,
'message' => 'Please provide required url parameters.',
], 403);
});
}
The above test keeps failing, but i can't seem to know why. I just need a way to output the real error messages, so i can move forward.
Thanks
I'm using Laravel version 8.8
Aucun commentaire:
Enregistrer un commentaire