繁体   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