预期状态代码 200,但收到 302。无法断言 false 为 true。在 Laravel 5.4 中使用 phpunit 测试

Sof*_*per 4 phpunit laravel-5

您好,我有这个页面/dno-personal/cebu-properties。我尝试在我的 laravel 中使用 PHPUNIT 测试运行。现在我在下面创建了这个测试文件

<?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);

    }
}
Run Code Online (Sandbox Code Playgroud)

现在,在我的路线文件中,ii 没有为 dno-personal/cebu-properties 创建路线,但我在 phpunit 中运行测试,它会抛出以下错误

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.
Run Code Online (Sandbox Code Playgroud)

我认为这是可以的,因为我还没有路线,这会引发 404 错误。现在当我添加到路线中时

Route::get('/dno-personal/cebu-properties',
        'DnoPersonalController@cebuProperties')
        ->name('dno-personal.cebuProperties');
Run Code Online (Sandbox Code Playgroud)

当我运行测试 PHPUNIT 时,我的控制器 cebuProperties 中没有方法,它会抛出

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.
Run Code Online (Sandbox Code Playgroud)

它抛出 302 错误。现在我希望它抛出方法尚未创建而不是 302。现在当我添加$this->withoutExceptionHandling();此错误时会抛出错误

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
Run Code Online (Sandbox Code Playgroud)

它看不到$this->withoutExceptionHandling();有人能帮我解决这个问题吗?非常感谢任何帮助。TIA

Rem*_*mul 9

问题是您的路由应用了中间件auth,这意味着请求在到达您的路由之前就被重定向到登录页面。

您必须创建并验证用户才能正确测试此路由,Laravel 有actingAs此帮助程序。

来自文档

当然,会话的一种常见用途是维护经过身份验证的用户的状态。actAs 帮助器方法提供了一种简单的方法来将给定用户验证为当前用户。例如,我们可以使用模型工厂来生成和验证用户:

<?php

use App\User;

class ExampleTest extends TestCase
{
    public function testApplication()
    {
        $user = factory(User::class)->create();

        $response = $this->actingAs($user)
                         ->withSession(['foo' => 'bar'])
                         ->get('/');
    }
}
Run Code Online (Sandbox Code Playgroud)


Den*_*ian 5

当我测试一个应该返回 422 状态的案例(这是一个验证案例)时,就会发生这种情况。我通过在测试中定义标头解决了这个问题。Accept: application/json所以请确保像这样使用:

        $this
            ->withHeaders(['Accept' => 'application/json'])
            ->post("api/{$this->apiVersion}/auth/register", $requestBody)
            ->assertStatus(422);
Run Code Online (Sandbox Code Playgroud)

参考:https: //laravel.com/docs/9.x/sanctum#spa-authentication