gpt4 book ai didi

laravel - 时间戳使我的 phpunit 测试失败

转载 作者:行者123 更新时间:2023-12-05 05:07:06 25 4
gpt4 key购买 nike

当我使用以下 PostResource 和 Post Test 时,我的测试成功了:

PostResource.php

public function toArray($request)
{
return [
'id' => $this->id,
'title' => $this->title,
'content' => $this->content,
'slug' => $this->slug,
];
}

PostController.php

public function show(Post $post)
{
return new PostResource($post);
}

ReadPostTest.php

/** @test */
public function a_user_can_read_a_single_post()
{
$post = factory(Post::class)->create([
'title' => 'A new post',
'content' => "Some content",
'slug' => "a-new-post",
]);

//dd($post);

$response = $this->json('GET', '/posts/' . $post->id)
->assertStatus(200)
->assertJsonFragment([ 'data' => [
'title' => 'A new post',
'content' => "Some content",
'slug' => "a-new-post",
]]);

}

当我将 created_at 和 updated_at 添加到我的测试和资源时,我失败了。当我没有添加 .000000Z 时,下面的测试显示。

phpunit

There was 1 failure:

1) Tests\Feature\ReadPostsTest::a_user_can_read_a_single_post
Unable to find JSON:

[{
"data": {
"title": "A new post",
"content": "Some content",
"slug": "a-new-post",
"created_at": "2018-12-06 21:13:26",
"updated_at": "2018-12-06 21:13:26"
}
}]

within response JSON:

[{
"data": {
"id": 1,
"title": "A new post",
"content": "Some content",
"slug": "a-new-post",
"created_at": "2018-12-06T21:13:26.000000Z",
"updated_at": "2018-12-06T21:13:26.000000Z"
}
}].


Failed asserting that an array has the subset Array &0 (
'data' => Array &1 (
'title' => 'A new post'
'content' => 'Some content'
'slug' => 'a-new-post'
'created_at' => '2018-12-06 21:13:26'
'updated_at' => '2018-12-06 21:13:26'
)
).
--- Expected
+++ Actual
@@ @@
'title' => 'A new post',
'content' => 'Some content',
'slug' => 'a-new-post',
- 'created_at' => '2018-12-06 21:13:26',
- 'updated_at' => '2018-12-06 21:13:26',
+ 'created_at' => '2018-12-06T21:13:26.000000Z',
+ 'updated_at' => '2018-12-06T21:13:26.000000Z',
),
)

我尝试添加 000000Z 并遇到同样的问题。

There was 1 failure:

1) Tests\Feature\ReadPostsTest::a_user_can_read_a_single_post
Unable to find JSON fragment:

[{"data":{"content":"Some content","created_at":"2019-12-20 21:42:33.000000Z","id":1,"slug":"a-new-post","title":"A new post","updated_at":"2019-12-20 21:42:33.000000Z"}}]

within

[{"data":{"content":"Some content","created_at":"2019-12-20T21:42:33.000000Z","id":1,"slug":"a-new-post","title":"A new post","updated_at":"2019-12-20T21:42:33.000000Z"}}].
Failed asserting that false is true.

似乎我的 created_at 和 up_dated at 时间戳被弄乱了,我不知道为什么? 2019-12-20T21:42:33.000000Z这可能是导致我的测试失败的原因。我该如何解决这个问题?

最佳答案

在 post 模型工厂上使用 carbon,然后在 json 断言中使用 $post->created_at$post->updated_at:

public function a_user_can_read_a_single_post()
{
$post = factory(Post::class)->create([
'title' => 'A new post',
'content' => "Some content",
'slug' => "a-new-post",
"updated_at" => Carbon::now()->timestamp,
"created_at" => Carbon::now()->timestamp
]);

//dd($post);

$response = $this->json('GET', '/posts/' . $post->id)
->assertStatus(200)
->assertJsonFragment([ 'data' => [
'id' => 1,
'title' => 'A new post',
'content' => "Some content",
'slug' => "a-new-post",
"updated_at" => $post->updated_at,
"created_at" => $post->created_at
]]);

}

由于某种原因,一串时间戳不起作用。希望其他人可以评论原因。

关于laravel - 时间戳使我的 phpunit 测试失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59431833/

25 4 0
文章推荐: powershell - 在脚本 block powershell 中调用函数
文章推荐: javascript - 使用 js-cookie 和 JSON.parse 从 cookie 中设置和获取数组
文章推荐: laravel - Laravel Resource 中的附加数据
文章推荐: reactjs - 变体 ="filled"不适用于