11
11
using JsonApiDotNetCore . Models ;
12
12
using JsonApiDotNetCoreExample . Data ;
13
13
using System . Linq ;
14
+ using Bogus ;
15
+ using JsonApiDotNetCoreExample . Models ;
14
16
15
17
namespace JsonApiDotNetCoreExampleTests . Acceptance . Spec . DocumentTests
16
18
{
@@ -19,10 +21,15 @@ public class Relationships
19
21
{
20
22
private DocsFixture < Startup , JsonDocWriter > _fixture ;
21
23
private AppDbContext _context ;
24
+ private Faker < TodoItem > _todoItemFaker ;
25
+
22
26
public Relationships ( DocsFixture < Startup , JsonDocWriter > fixture )
23
27
{
24
28
_fixture = fixture ;
25
29
_context = fixture . GetService < AppDbContext > ( ) ;
30
+ _todoItemFaker = new Faker < TodoItem > ( )
31
+ . RuleFor ( t => t . Description , f => f . Lorem . Sentence ( ) )
32
+ . RuleFor ( t => t . Ordinal , f => f . Random . Number ( ) ) ;
26
33
}
27
34
28
35
[ Fact ]
@@ -31,18 +38,22 @@ public async Task Correct_RelationshipObjects_For_ManyToOne_Relationships()
31
38
// arrange
32
39
var builder = new WebHostBuilder ( )
33
40
. UseStartup < Startup > ( ) ;
41
+
42
+ var todoItem = _todoItemFaker . Generate ( ) ;
43
+ _context . TodoItems . Add ( todoItem ) ;
44
+ await _context . SaveChangesAsync ( ) ;
34
45
35
46
var httpMethod = new HttpMethod ( "GET" ) ;
36
- var route = $ "/api/v1/todo-items";
47
+ var route = $ "/api/v1/todo-items/ { todoItem . Id } ";
37
48
38
49
var server = new TestServer ( builder ) ;
39
50
var client = server . CreateClient ( ) ;
40
51
var request = new HttpRequestMessage ( httpMethod , route ) ;
41
52
42
53
// act
43
54
var response = await client . SendAsync ( request ) ;
44
- var documents = JsonConvert . DeserializeObject < Documents > ( await response . Content . ReadAsStringAsync ( ) ) ;
45
- var data = documents . Data [ 0 ] ;
55
+ var document = JsonConvert . DeserializeObject < Document > ( await response . Content . ReadAsStringAsync ( ) ) ;
56
+ var data = document . Data ;
46
57
var expectedOwnerSelfLink = $ "http://localhost/api/v1/todo-items/{ data . Id } /relationships/owner";
47
58
var expectedOwnerRelatedLink = $ "http://localhost/api/v1/todo-items/{ data . Id } /owner";
48
59
@@ -56,13 +67,15 @@ public async Task Correct_RelationshipObjects_For_ManyToOne_Relationships()
56
67
public async Task Correct_RelationshipObjects_For_ManyToOne_Relationships_ById ( )
57
68
{
58
69
// arrange
59
- var todoItemId = _context . TodoItems . Last ( ) . Id ;
60
-
61
70
var builder = new WebHostBuilder ( )
62
71
. UseStartup < Startup > ( ) ;
72
+
73
+ var todoItem = _todoItemFaker . Generate ( ) ;
74
+ _context . TodoItems . Add ( todoItem ) ;
75
+ await _context . SaveChangesAsync ( ) ;
63
76
64
77
var httpMethod = new HttpMethod ( "GET" ) ;
65
- var route = $ "/api/v1/todo-items/{ todoItemId } ";
78
+ var route = $ "/api/v1/todo-items/{ todoItem . Id } ";
66
79
67
80
var server = new TestServer ( builder ) ;
68
81
var client = server . CreateClient ( ) ;
@@ -72,8 +85,8 @@ public async Task Correct_RelationshipObjects_For_ManyToOne_Relationships_ById()
72
85
var response = await client . SendAsync ( request ) ;
73
86
var responseString = await response . Content . ReadAsStringAsync ( ) ;
74
87
var data = JsonConvert . DeserializeObject < Document > ( responseString ) . Data ;
75
- var expectedOwnerSelfLink = $ "http://localhost/api/v1/todo-items/{ todoItemId } /relationships/owner";
76
- var expectedOwnerRelatedLink = $ "http://localhost/api/v1/todo-items/{ todoItemId } /owner";
88
+ var expectedOwnerSelfLink = $ "http://localhost/api/v1/todo-items/{ todoItem . Id } /relationships/owner";
89
+ var expectedOwnerRelatedLink = $ "http://localhost/api/v1/todo-items/{ todoItem . Id } /owner";
77
90
78
91
// assert
79
92
Assert . Equal ( HttpStatusCode . OK , response . StatusCode ) ;
0 commit comments