最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Why Pest test on Laravel always fail (404) with valid URL? - Stack Overflow

programmeradmin3浏览0评论

I am adding a Pest test on Laravel 11 / Livewire 3 site

I created a new test file with command :

php artisan pest:test UsersTest

But adding any code to check existing page I got 404 error, like in file tests/Feature/UsersTest.php. Making checks :

<?php

use function Pest\Faker\fake;

it('has contact-us page', function () {
    $response = $this->get('contact-us');

    $response->assertStatus(200);
});

I got failed testing results :

 ./vendor/bin/pest

   FAIL  Tests\Feature\UsersTest
  ⨯ it has contact-us page                                                                                                                                                                                                           0.39s
  ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
   FAILED  Tests\Feature\UsersTest > it has contact-us page
  Expected response status code [200] but received 404.
Failed asserting that 404 is identical to 200.

  at tests/Feature/UsersTest.php:8
      4▕
      5▕ it('has contact-us page', function () {
      6▕     $response = $this->get('contact-us');
      7▕
  ➜   8▕     $response->assertStatus(200);
      9▕


  Tests:    1 failed (1 assertions)

But in routes/web.php there is a row :

Route::get('/contact-us', ContactUsPage::Class)->name('contact-us');

and checking command :

php artisan route:list

I have line :

GET|HEAD  contact-us ........................................................................................................................................................................... contact-us › App\Livewire\ContactUsPage

and in tests/Pest.php :

<?php

/*
|--------------------------------------------------------------------------
| Test Case
|--------------------------------------------------------------------------
|
| The closure you provide to your test functions is always bound to a specific PHPUnit test
| case class. By default, that class is "PHPUnit\Framework\TestCase". Of course, you may
| need to change it using the "pest()" function to bind a different classes or traits.
|
*/

pest()->extend(Tests\TestCase::class)
 // ->use(Illuminate\Foundation\Testing\RefreshDatabase::class)
    ->in('Feature');

/*
|--------------------------------------------------------------------------
| Expectations
|--------------------------------------------------------------------------
|
| When you're writing tests, you often need to check that values meet certain conditions. The
| "expect()" function gives you access to a set of "expectations" methods that you can use
| to assert different things. Of course, you may extend the Expectation API at any time.
|
*/

expect()->extend('toBeOne', function () {
    return $this->toBe(1);
});

/*
|--------------------------------------------------------------------------
| Functions
|--------------------------------------------------------------------------
|
| While Pest is very powerful out-of-the-box, you may have some testing code specific to your
| project that you don't want to repeat in every file. Here you can also expose helpers as
| global functions to help you to reduce the number of lines of code in your test files.
|
*/

function something()
{
    // ..
}

I checked several existing routs with same fail.

What is wrong with my PEST ?

    "livewire/livewire": "^3.5",
    "laravel/framework": "^11.9",
    "pestphp/pest-plugin-faker": "^3.0",
    "pestphp/pest": "^3.7",
    "pestphp/pest-plugin-laravel": "^3.0",
发布评论

评论列表(0)

  1. 暂无评论