簡體   English   中英

Laravel 測試使用 PHPUnit 更新數據,斷言兩個字符串相等失敗

[英]Laravel test using PHPUnit update data, Failed asserting that two strings are equal

我試圖在 laravel 上學習 PHPUnit,但遇到了一個小問題。 我設法對數據輸入和數據驗證進行了測試,但以某種方式更新數據卻不起作用。

這是我在控制器中的更新功能

public function update(PostRequest $request, PostModel $post)
    {

        $update = $request->all();

        $update['category_id'] = $request->category;

        $post->update($update);

        $post->TagModels()->sync($request->tag);

        session()->flash('success', 'Update Post Success');

        return redirect('post');
    }

這是我的更新路線

Route::patch('post/{post:slug}/update', 'PostController@update');

這是我的更新測試

public function test_post_update()
    {
        // $this->withoutExceptionHandling();

        $data = factory(PostModel::class)->make();

        $response = $this->post('post/store', [
            'title'=> $data->title,

            'body'=> $data->body,

            'category'=> $data->category_id,

            'tag'=> [rand(1, 5)],
        ]);

        $post = PostModel::first()->slug;

        // $data2 = factory(PostModel::class)->make();

        $this->patch('post/'.$post.'/update', [
            'title'=> 'update',

            'body'=> $data->body,

            'category'=> $data->category_id,

            'tag'=> [rand(1, 5)],
        ]);

        $data->refresh();

        $this->assertEquals('update', PostModel::first()->title);
    }

我總是收到以下錯誤

> Executing task: c:/xampp/htdocs/my-app-test/vendor/bin/phpunit.bat c:/xampp/htdocs/my-app-test/tests/Unit/PostTest.php --filter '^.*::test_post_update' <

PHPUnit 8.5.9 by Sebastian Bergmann and contributors.

F                                                                   1 / 1 (100%)

Time: 334 ms, Memory: 24.00 MB

There was 1 failure:

1) Tests\Unit\PostTest::test_post_update
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'update'
+'Laudantium voluptatibus voluptatem ipsam sit.'

C:\xampp\htdocs\my-app-test\tests\Unit\PostTest.php:216

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.

請告訴我我的代碼哪里出錯了。

取消注釋$this->withoutExceptionHandling();后的新錯誤$this->withoutExceptionHandling();

這是新的錯誤

1) Tests\Unit\PostTest::test_post_update
Illuminate\Database\QueryException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'post_id' cannot be null (SQL: insert into `post_tag` (`post_id`, `tag_id`) values (?, 1))

嘗試調整更新方法以處理除tagscategory之外的所有請求數據字段

public function update(PostRequest $request, PostModel $post)

{

    //(dd($request->all());

    //authorise the logged in user to perform the update

    //Validate the request data


    $post->category_id = $request->category;

    $post->update($request->except(['category', 'tags']));

    $post->TagModels()->sync($request->tag);

    session()->flash('success', 'Update Post Success');

    return redirect('post');
} 

讓我們稍微調整一下測試

ublic function test_post_update()
    {
        // $this->withoutExceptionHandling();

        $data = factory(PostModel::class)->make();

        $response = $this->post('post/store', [
            'title'=> $data->title,

            'body'=> $data->body,

            'category'=> $data->category_id,

            'tag'=> [rand(1, 5)],
        ]);

        $post = PostModel::first();

        // $data2 = factory(PostModel::class)->make();

        $this->patch('post/' . $post->slug . '/update', [
            'title'=> 'update',

            'body'=> $data->body,

            'category'=> $data->category_id,

            'tag'=> [rand(1, 5)],
        ]);

        $post->refresh();

        $this->assertEquals('update', $post->title);
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM