Reputation: 197
Hi i have this page which is /dno-personal/cebu-properties. I tried to run in using PHPUNIT test in my laravel. Now i created this test file below
<?php
namespace Tests\Feature;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Http\Response;
class DnoPersonalTest extends TestCase
{
/**
* A basic test example.
*
* @test
*/
public function add_cebu_properties_page()
{
$response = $this->get('/dno-personal/cebu-properties');
$response->assertStatus(200);
}
}
Now in my route file i i did not create a route for dno-personal/cebu-properties yet which i run test into my phpunit it throws an error of
Expected status code 200 but received 404.
Failed asserting that false is true.
C:\xampp\htdocs\dnogroup\vendor\laravel\framework\src\Illuminate\Foundation\Testing\TestResponse.php:79
C:\xampp\htdocs\dnogroup\tests\Feature\DnoPersonalTest.php:24
FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
which i think is okay cause i have no route yet which throws an error of 404. Now when i add into the route
Route::get('/dno-personal/cebu-properties',
'DnoPersonalController@cebuProperties')
->name('dno-personal.cebuProperties');
without a method into my controller cebuProperties when i run test PHPUNIT it throws
Expected status code 200 but received 302.
Failed asserting that false is true.
C:\xampp\htdocs\dnogroup\vendor\laravel\framework\src\Illuminate\Foundation\Testing\TestResponse.php:79
C:\xampp\htdocs\dnogroup\tests\Feature\DnoPersonalTest.php:24
FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
it throws an error of 302. Now i want that it throws the method is not yet created instead of 302.
now when i add $this->withoutExceptionHandling();
this throws me an error
PHP Fatal error: Call to undefined method Tests\Feature\DnoPersonalTest::withoutExceptionHandling() in C:\xampp\htdocs\dnogroup\tests\Feature\DnoPersonalTest.php on line 22
In DnoPersonalTest.php line 22:
Call to undefined method Tests\Feature\DnoPersonalTest::withoutExceptionHandling()
Fatal error: Call to undefined method Tests\Feature\DnoPersonalTest::withoutExceptionHandling() in C:\xampp\htdocs\dnogroup\tests\Feature\DnoPersonalTest.php on line 22
it cannot see the $this->withoutExceptionHandling();
can someone help me figured this out? Any help is muchly appreciated. TIA
Upvotes: 4
Views: 16248
Reputation: 41
In my case I was using a personalized request that redirects, because the validate fails. I set the default request and works fine, before I review the data and my personalized request works fine too.
Upvotes: 0
Reputation: 374
This happens when I test a case that should return 422 status, which is a validation case. I solved it by defining the headers in the test. So make sure to use Accept: application/json
like this:
$this
->withHeaders(['Accept' => 'application/json'])
->post("api/{$this->apiVersion}/auth/register", $requestBody)
->assertStatus(422);
ref: https://laravel.com/docs/9.x/sanctum#spa-authentication
Upvotes: 5
Reputation: 8252
The problem is that your route applies the auth
middleware, which means that the request is redirect to the login page before even reaching your route.
You will have to create and authenticate a user to test this route properly, Laravel has the actingAs
helper for this.
From the docs:
Of course, one common use of the session is for maintaining state for the authenticated user. The actingAs helper method provides a simple way to authenticate a given user as the current user. For example, we may use a model factory to generate and authenticate a user:
<?php
use App\User;
class ExampleTest extends TestCase
{
public function testApplication()
{
$user = factory(User::class)->create();
$response = $this->actingAs($user)
->withSession(['foo' => 'bar'])
->get('/');
}
}
Upvotes: 10