Skip to content

Commit 23e266a

Browse files
author
Piotr Jura
committed
#210 Testing JSON structure deeply
1 parent 0821b66 commit 23e266a

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

tests/Feature/ApiPostCommentsTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Foundation\Testing\WithFaker;
77
use Illuminate\Foundation\Testing\RefreshDatabase;
88
use App\BlogPost;
9+
use App\Comment;
910

1011
class ApiPostCommentsTest extends TestCase
1112
{
@@ -23,4 +24,40 @@ public function testNewBlogPostDoesNotHaveComments()
2324
->assertJsonStructure(['data', 'links', 'meta'])
2425
->assertJsonCount(0, 'data');
2526
}
27+
28+
public function testBlogPostHas10Comments()
29+
{
30+
factory(BlogPost::class)->create([
31+
'user_id' => $this->user()->id
32+
])->each(function (BlogPost $post) {
33+
$post->comments()->saveMany(
34+
factory(Comment::class, 10)->make([
35+
'user_id' => $this->user()->id
36+
])
37+
);
38+
});
39+
40+
$response = $this->json('GET', 'api/v1/posts/2/comments');
41+
42+
$response->assertStatus(200)
43+
->assertJsonStructure(
44+
[
45+
'data' => [
46+
'*' => [
47+
'id',
48+
'contnt',
49+
'created_at',
50+
'updated_at',
51+
'user' => [
52+
'id',
53+
'name'
54+
]
55+
]
56+
],
57+
'links',
58+
'meta'
59+
]
60+
)
61+
->assertJsonCount(10, 'data');
62+
}
2663
}

0 commit comments

Comments
 (0)