我应该使用哪种断言方法来通过 api 存储测试?

发布于 2025-01-10 19:12:37 字数 2099 浏览 0 评论 0原文

我正在尝试传递 testStoreUser() 方法,但在使用哪个断言语句时遇到问题。我使用 API 路由而不是带有哈希的服务器路由的密码。 这是我的 testStoreUser():

 public function testStoreUser()
    {
        //Arrange
        $data = [
            'data' => [
                'name' => 'Testing Name',
                'email' => '[email protected]',
                'password' => 'Harley81',
            ],
        ];

        //Act
        $response = $this->postJson('/api/users/', $data);

        //Assert
        $response
            ->assertStatus(201)
            ->assertJsonStructure([
                'data' => [
                    'name',
                    'email',
                    'password',
                    ]
            ]);

        $this->assertDatabaseHas('users', $data['data']);
    }

这是我的 api/用户控制器,特别是存储函数:

 /**
     * Store a newly created user in storage.
     *
     * @param Request $request
     * @return UserResource
     */
    public function store(Request $request): UserResource
    {
        $validated = $request->validate([
            'data' => 'required|array',
            'data.name'=> 'required|string',
            'data.email'=> 'required|string',
            'data.password' => ['required', Password::min(8)->mixedCase()],
        ]);

        $user = new User();

        $user->fill($validated['data'])->saveOrFail();

        return new UserResource($user);
    }

这是我收到的当前错误:

Failed asserting that an array has the key 'password'.
 /var/www/html/vendor/laravel/framework/src/Illuminate/Testing/AssertableJsonString.php:256
 /var/www/html/vendor/laravel/framework/src/Illuminate/Testing/AssertableJsonString.php:241
 /var/www/html/vendor/laravel/framework/src/Illuminate/Testing/AssertableJsonString.php:254
 /var/www/html/vendor/laravel/framework/src/Illuminate/Testing/TestResponse.php:795
 /var/www/html/tests/Feature/UserApiTest.php:49

I am trying to pass my testStoreUser() method and am having issues with which assert statement to use. The password I am using the API route instead to the server route with hash.
Here is my testStoreUser():

 public function testStoreUser()
    {
        //Arrange
        $data = [
            'data' => [
                'name' => 'Testing Name',
                'email' => '[email protected]',
                'password' => 'Harley81',
            ],
        ];

        //Act
        $response = $this->postJson('/api/users/', $data);

        //Assert
        $response
            ->assertStatus(201)
            ->assertJsonStructure([
                'data' => [
                    'name',
                    'email',
                    'password',
                    ]
            ]);

        $this->assertDatabaseHas('users', $data['data']);
    }

Here is my api/user controller specifically the store function:

 /**
     * Store a newly created user in storage.
     *
     * @param Request $request
     * @return UserResource
     */
    public function store(Request $request): UserResource
    {
        $validated = $request->validate([
            'data' => 'required|array',
            'data.name'=> 'required|string',
            'data.email'=> 'required|string',
            'data.password' => ['required', Password::min(8)->mixedCase()],
        ]);

        $user = new User();

        $user->fill($validated['data'])->saveOrFail();

        return new UserResource($user);
    }

And this is current error I am getting:

Failed asserting that an array has the key 'password'.
 /var/www/html/vendor/laravel/framework/src/Illuminate/Testing/AssertableJsonString.php:256
 /var/www/html/vendor/laravel/framework/src/Illuminate/Testing/AssertableJsonString.php:241
 /var/www/html/vendor/laravel/framework/src/Illuminate/Testing/AssertableJsonString.php:254
 /var/www/html/vendor/laravel/framework/src/Illuminate/Testing/TestResponse.php:795
 /var/www/html/tests/Feature/UserApiTest.php:49

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野却迷人 2025-01-17 19:12:37

我在测试中只需要这个。

      $response
            ->assertStatus(201)
            ->assertJson([
                'data' => [
                    'name' => $data['data']['name'],
                    'email' => $data['data']['email'],
                ]
            ]);

I just needed this in my test.

      $response
            ->assertStatus(201)
            ->assertJson([
                'data' => [
                    'name' => $data['data']['name'],
                    'email' => $data['data']['email'],
                ]
            ]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文