Laravel 功能测试,如何测试用户是否创建了个人资料页面?

Laravel Feature testing, how do i test if a profile page has been created with the user?

我创建了我的功能测试;

ProfilesControllerTest.php

<?php

namespace Tests\Feature;

use App\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;

class ProfileControllerTest extends TestCase
{
    use RefreshDatabase;

    /**
     * A basic feature test example.
     *
     * @return void
     */
    public function test_the_profile_page_is_rendered()
    {
        // First The user is created
        $user = User::factory()->create();

        //act as user
        $this->actingAs($user);

        // Then we want to make sure a profile page is created
        $response = $this->get('/profile/{user}');

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

web.php

Route::get('/profile/{user}', 'ProfilesController@index')->name('profiles.show');

但是一直报错。我怀疑这是因为配置文件 link,但我不确定如何显示它。我尝试了一些变体,但未能成功。

我意识到工厂无法正常工作,所以我尝试了这个;

ProfilesControllerTest.php

public function test_the_profile_page_is_rendered()
{
    // First The user is created
    $user = User::make([
        'name' => 'John Doe',
        'username' => 'johnnyd',
        'email' => 'johndoe@email.com'
    ]);

    //act as user
    $this->actingAs($user);

    // Then we want to make sure a profile page is created
    $response = $this->get('/profile/{$user}');

    $response->assertStatus(200);
}

而且我一直收到错误消息:

错误

php artisan test

 PASS  Tests\Unit\ExampleTest
✓ basic test

 PASS  Tests\Unit\UserTest
✓ login form
✓ user duplication

 PASS  Tests\Feature\ExampleTest
✓ basic test

 FAIL  Tests\Feature\ProfileControllerTest
✕ the profile page is rendered

Tests:  1 failed, 4 passed, 1 pending

Expected status code 200 but received 404. Failed asserting that 200 is identical to 404.

at tests/Feature/ProfileControllerTest.php:34
  30|         // Then we want to make sure a profile page is created
  31|         $response = $this->get('/profile/{$user');
  32| 
  33|         //

> 34|         $response->assertStatus(200);
  35|     }
  36| }
  37|

索引的配置文件控制器编写如下:

ProfilesController.php

class ProfilesController extends Controller
{
    public function index(User $user)
    {
        $postCount = Cache::remember(
            'count.posts.' . $user->id,
            now()->addSeconds(30),
            function () use ($user) {
                return $user->posts->count();
            }
        );

        return view('profiles.index', compact('user', 'postCount'));
    }
}

您的第一个测试没有成功,因为您试图访问错误的 URL。您正在尝试前往 http://localhost/profile/{user}。 URL 不正确,因为没有 ID 为“{user}”的用户。您要访问的URL是http://localhost/profile/1,查看id为1的用户的个人资料。

要修复第一个测试,请修复 URL:

// bad
// $response = $this->get('/profile/{user}');

// good
$response = $this->get('/profile/'.$user->id);

您的第二次测试失败有两个原因:

  1. User::make() 将生成 User 模型的新实例,但它不会将任何内容保存到数据库中。由于 User 不会存在于数据库中,因此它没有您可以访问的配置文件 URL。
  2. 同样,与第一次测试一样,您尝试访问的配置文件 URL 是错误的。

所以,回到第一个测试,纠正URL,你应该可以了。