10
10
using JsonApiDotNetCore . Models ;
11
11
using JsonApiDotNetCoreExample . Data ;
12
12
using JsonApiDotNetCoreExample . Models ;
13
+ using JsonApiDotNetCoreExampleTests . Helpers . Models ;
13
14
using Microsoft . EntityFrameworkCore ;
14
15
using Newtonsoft . Json ;
15
16
using Xunit ;
@@ -101,9 +102,9 @@ public async Task Can_Filter_By_Relationship_Id()
101
102
{
102
103
// Arrange
103
104
var person = new Person ( ) ;
104
- var todoItem = _todoItemFaker . Generate ( ) ;
105
- todoItem . Owner = person ;
106
- _context . TodoItems . Add ( todoItem ) ;
105
+ var todoItems = _todoItemFaker . Generate ( 3 ) . ToList ( ) ;
106
+ _context . TodoItems . AddRange ( todoItems ) ;
107
+ todoItems [ 0 ] . Owner = person ;
107
108
_context . SaveChanges ( ) ;
108
109
109
110
var httpMethod = new HttpMethod ( "GET" ) ;
@@ -118,7 +119,7 @@ public async Task Can_Filter_By_Relationship_Id()
118
119
// Assert
119
120
Assert . Equal ( HttpStatusCode . OK , response . StatusCode ) ;
120
121
Assert . NotEmpty ( deserializedBody ) ;
121
- Assert . Contains ( deserializedBody , ( i ) => i . Owner . Id == person . Id ) ;
122
+ Assert . Contains ( deserializedBody , ( i ) => i . Id == todoItems [ 0 ] . Id ) ;
122
123
}
123
124
124
125
[ Fact ]
@@ -456,39 +457,17 @@ public async Task Can_Post_TodoItem()
456
457
_context . People . Add ( person ) ;
457
458
_context . SaveChanges ( ) ;
458
459
460
+ var serializer = _fixture . GetSerializer < TodoItem > ( e => new { e . Description , e . OffsetDate , e . Ordinal , e . CreatedDate } , e => new { e . Owner } ) ;
461
+
459
462
var todoItem = _todoItemFaker . Generate ( ) ;
460
463
var nowOffset = new DateTimeOffset ( ) ;
461
- var content = new
462
- {
463
- data = new
464
- {
465
- type = "todo-items" ,
466
- attributes = new Dictionary < string , object > ( )
467
- {
468
- { "description" , todoItem . Description } ,
469
- { "ordinal" , todoItem . Ordinal } ,
470
- { "created-date" , todoItem . CreatedDate } ,
471
- { "offset-date" , nowOffset }
472
- } ,
473
- relationships = new
474
- {
475
- owner = new
476
- {
477
- data = new
478
- {
479
- type = "people" ,
480
- id = person . Id . ToString ( )
481
- }
482
- }
483
- }
484
- }
485
- } ;
464
+ todoItem . OffsetDate = nowOffset ;
486
465
487
466
var httpMethod = new HttpMethod ( "POST" ) ;
488
467
var route = $ "/api/v1/todo-items";
489
468
490
469
var request = new HttpRequestMessage ( httpMethod , route ) ;
491
- request . Content = new StringContent ( JsonConvert . SerializeObject ( content ) ) ;
470
+ request . Content = new StringContent ( serializer . Serialize ( todoItem ) ) ;
492
471
request . Content . Headers . ContentType = new MediaTypeHeaderValue ( "application/vnd.api+json" ) ;
493
472
494
473
// Act
@@ -497,7 +476,7 @@ public async Task Can_Post_TodoItem()
497
476
// Assert
498
477
Assert . Equal ( HttpStatusCode . Created , response . StatusCode ) ;
499
478
var body = await response . Content . ReadAsStringAsync ( ) ;
500
- var deserializedBody = _fixture . GetDeserializer ( ) . DeserializeSingle < TodoItem > ( body ) . Data ;
479
+ var deserializedBody = _fixture . GetDeserializer ( ) . DeserializeSingle < TodoItemClient > ( body ) . Data ;
501
480
Assert . Equal ( HttpStatusCode . Created , response . StatusCode ) ;
502
481
Assert . Equal ( todoItem . Description , deserializedBody . Description ) ;
503
482
Assert . Equal ( todoItem . CreatedDate . ToString ( "G" ) , deserializedBody . CreatedDate . ToString ( "G" ) ) ;
0 commit comments