I was trying to learn PHPUnit on laravel and I got a little problem. I managed to make a test for data input and data validation but for updating the data somehow it didn't work.
This is my update function in controller
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');
}
This my route for update
Route::patch('post/{post:slug}/update', 'PostController@update');
This my test for 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);
}
I always get the following error
> 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.
Please tell me where I went wrong in my code.
New error after i uncomment $this->withoutExceptionHandling();
This the new error
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))
Try to tweak the update method to deal with all request data fields except tags
and category
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');
}
Let us tweak the test a little
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);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.