1111using JsonApiDotNetCore . Models ;
1212using JsonApiDotNetCoreExample . Data ;
1313using System . Linq ;
14+ using Bogus ;
15+ using JsonApiDotNetCoreExample . Models ;
1416
1517namespace JsonApiDotNetCoreExampleTests . Acceptance . Spec . DocumentTests
1618{
@@ -19,10 +21,15 @@ public class Relationships
1921 {
2022 private DocsFixture < Startup , JsonDocWriter > _fixture ;
2123 private AppDbContext _context ;
24+ private Faker < TodoItem > _todoItemFaker ;
25+
2226 public Relationships ( DocsFixture < Startup , JsonDocWriter > fixture )
2327 {
2428 _fixture = fixture ;
2529 _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 ( ) ) ;
2633 }
2734
2835 [ Fact ]
@@ -31,18 +38,22 @@ public async Task Correct_RelationshipObjects_For_ManyToOne_Relationships()
3138 // arrange
3239 var builder = new WebHostBuilder ( )
3340 . UseStartup < Startup > ( ) ;
41+
42+ var todoItem = _todoItemFaker . Generate ( ) ;
43+ _context . TodoItems . Add ( todoItem ) ;
44+ await _context . SaveChangesAsync ( ) ;
3445
3546 var httpMethod = new HttpMethod ( "GET" ) ;
36- var route = $ "/api/v1/todo-items";
47+ var route = $ "/api/v1/todo-items/ { todoItem . Id } ";
3748
3849 var server = new TestServer ( builder ) ;
3950 var client = server . CreateClient ( ) ;
4051 var request = new HttpRequestMessage ( httpMethod , route ) ;
4152
4253 // act
4354 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 ;
4657 var expectedOwnerSelfLink = $ "http://localhost/api/v1/todo-items/{ data . Id } /relationships/owner";
4758 var expectedOwnerRelatedLink = $ "http://localhost/api/v1/todo-items/{ data . Id } /owner";
4859
@@ -56,13 +67,15 @@ public async Task Correct_RelationshipObjects_For_ManyToOne_Relationships()
5667 public async Task Correct_RelationshipObjects_For_ManyToOne_Relationships_ById ( )
5768 {
5869 // arrange
59- var todoItemId = _context . TodoItems . Last ( ) . Id ;
60-
6170 var builder = new WebHostBuilder ( )
6271 . UseStartup < Startup > ( ) ;
72+
73+ var todoItem = _todoItemFaker . Generate ( ) ;
74+ _context . TodoItems . Add ( todoItem ) ;
75+ await _context . SaveChangesAsync ( ) ;
6376
6477 var httpMethod = new HttpMethod ( "GET" ) ;
65- var route = $ "/api/v1/todo-items/{ todoItemId } ";
78+ var route = $ "/api/v1/todo-items/{ todoItem . Id } ";
6679
6780 var server = new TestServer ( builder ) ;
6881 var client = server . CreateClient ( ) ;
@@ -72,8 +85,8 @@ public async Task Correct_RelationshipObjects_For_ManyToOne_Relationships_ById()
7285 var response = await client . SendAsync ( request ) ;
7386 var responseString = await response . Content . ReadAsStringAsync ( ) ;
7487 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";
7790
7891 // assert
7992 Assert . Equal ( HttpStatusCode . OK , response . StatusCode ) ;
0 commit comments