Skip to content

Commit e5bbda2

Browse files
committed
fix: e2e controller tests
1 parent 90ab6e6 commit e5bbda2

File tree

1 file changed

+10
-31
lines changed

1 file changed

+10
-31
lines changed

test/JsonApiDotNetCoreExampleTests/Acceptance/TodoItemsControllerTests.cs

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using JsonApiDotNetCore.Models;
1111
using JsonApiDotNetCoreExample.Data;
1212
using JsonApiDotNetCoreExample.Models;
13+
using JsonApiDotNetCoreExampleTests.Helpers.Models;
1314
using Microsoft.EntityFrameworkCore;
1415
using Newtonsoft.Json;
1516
using Xunit;
@@ -101,9 +102,9 @@ public async Task Can_Filter_By_Relationship_Id()
101102
{
102103
// Arrange
103104
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;
107108
_context.SaveChanges();
108109

109110
var httpMethod = new HttpMethod("GET");
@@ -118,7 +119,7 @@ public async Task Can_Filter_By_Relationship_Id()
118119
// Assert
119120
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
120121
Assert.NotEmpty(deserializedBody);
121-
Assert.Contains(deserializedBody, (i) => i.Owner.Id == person.Id);
122+
Assert.Contains(deserializedBody, (i) => i.Id == todoItems[0].Id);
122123
}
123124

124125
[Fact]
@@ -456,39 +457,17 @@ public async Task Can_Post_TodoItem()
456457
_context.People.Add(person);
457458
_context.SaveChanges();
458459

460+
var serializer = _fixture.GetSerializer<TodoItem>(e => new { e.Description, e.OffsetDate, e.Ordinal, e.CreatedDate }, e => new { e.Owner });
461+
459462
var todoItem = _todoItemFaker.Generate();
460463
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;
486465

487466
var httpMethod = new HttpMethod("POST");
488467
var route = $"/api/v1/todo-items";
489468

490469
var request = new HttpRequestMessage(httpMethod, route);
491-
request.Content = new StringContent(JsonConvert.SerializeObject(content));
470+
request.Content = new StringContent(serializer.Serialize(todoItem));
492471
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.api+json");
493472

494473
// Act
@@ -497,7 +476,7 @@ public async Task Can_Post_TodoItem()
497476
// Assert
498477
Assert.Equal(HttpStatusCode.Created, response.StatusCode);
499478
var body = await response.Content.ReadAsStringAsync();
500-
var deserializedBody = _fixture.GetDeserializer().DeserializeSingle<TodoItem>(body).Data;
479+
var deserializedBody = _fixture.GetDeserializer().DeserializeSingle<TodoItemClient>(body).Data;
501480
Assert.Equal(HttpStatusCode.Created, response.StatusCode);
502481
Assert.Equal(todoItem.Description, deserializedBody.Description);
503482
Assert.Equal(todoItem.CreatedDate.ToString("G"), deserializedBody.CreatedDate.ToString("G"));

0 commit comments

Comments
 (0)