github laravel工作流程从路线中获得无效的JSON

发布于 2025-02-12 16:42:25 字数 2191 浏览 1 评论 0原文

对于A Laravel API的自动测试,我正在使用GitHub动作的“ Laravel”动作,这是GitHub动作做出的。 测试不断失败,告诉我 json从路线预期响应代码200但获得500,无法在null 上读取属性状态,在JSON

我正在使用Laravel Sanctum。可能是CSRF犯罪问题吗?

我的动作YML: https://gist.github.com/i2ejeffrey/77df8faac1b0f86623e2e2e2e2e2e44449f98d858

我的响应函数:示例示例:

     * success response method.
     *
     * @return \Illuminate\Http\Response
     */
    public function sendResponse($result, $message, $code = 200)
    {
        $response = [
            'success' => true,
            'data'    => $result, // result is most often one or 2 arrays
            'message' => $message,
        ];
        return response()->json($response, 200);
    }

示例测试:

    public function testSuccessfullyCreateAccommodationType()
    {
        $this->login(); // Login function that lots of tests need.

        $response = $this->postJson('/api/v5/accommodations/1/types', ['accommodation_name'=>$this->createName()]);

        $response
        ->assertJsonFragment(['success' => true])
        ->assertJsonStructure(['success', 'data' => 
        [], 'message']); // The array is filled with keys
    }

edit:edit:2我通过note nove code> note ::< href =“ https://gist.github.com/i2ejeffrey/da23bfbdf5fba1545456bd799a34f6276” rel =“ nofollow noreferrer“> https://gist.github.com/i2ejeffrey/da23bfbdf5fba1545456bd7999a34f6276

编辑2:我还获得以下警告:tty模式/dev/tty/tty/tty/dev/tty,

编辑3:客户端模型和客户端播种机。每当我运行测试时 https://gist.github.com/i2ejeffrey/39c779df217c9a75a7569f6f6f6fa3957d77 https://gist.github.com/i2ejeffrey/41CBBDBA8025D38547059D3A6F4D4392

For the automated testing of a laravel API I am using the 'laravel' action on github actions, the one made by github actions.
The tests keep failing telling me invalid JSON returned from route, expected response code 200 but got 500, cannot read property status on null and cannot find in json

I'm using laravel sanctum. Could it be a csrf-token problem?

My action yml: https://gist.github.com/I2EJeffrey/77df8faac1b0f86623e2e4449f98d858

My response function:

     * success response method.
     *
     * @return \Illuminate\Http\Response
     */
    public function sendResponse($result, $message, $code = 200)
    {
        $response = [
            'success' => true,
            'data'    => $result, // result is most often one or 2 arrays
            'message' => $message,
        ];
        return response()->json($response, 200);
    }

Example test:

    public function testSuccessfullyCreateAccommodationType()
    {
        $this->login(); // Login function that lots of tests need.

        $response = $this->postJson('/api/v5/accommodations/1/types', ['accommodation_name'=>$this->createName()]);

        $response
        ->assertJsonFragment(['success' => true])
        ->assertJsonStructure(['success', 'data' => 
        [], 'message']); // The array is filled with keys
    }

EDIT: 2 errors that I got by using withoutExceptionHandling: https://gist.github.com/I2EJeffrey/da23bfbdf5fba155456bd799a34f6276

EDIT 2: I also get the following warning: TTY mode requires /dev/tty to be read/writable.

EDIT 3: The client model and the client seeder. Whenever I run the tests a mysql docker container starts that gets a db migrated and seeded into it:
https://gist.github.com/I2EJeffrey/39c779df217c9a75a7569f6fa3957d77
https://gist.github.com/I2EJeffrey/41cbbdba8025d38547059d3a6f4d4392

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

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

发布评论

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

评论(1

不气馁 2025-02-19 16:42:25

我的问题是,由于没有添加$ this-&gt; call(clientseeder :: class); 在数据库中添加到数据库,因此DB没有被播种。这导致路线返回零,从而返回错误的JSON。

My problem was that the DB didn't get seeded due to not having added $this->call(ClientSeeder::class); to the DatabaseSeeder. Which caused the routes to return null and thus wrong json.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文