Skip to content

Commit e490683

Browse files
committed
test(*): fix test that are dependent on some items being defined
No tests should be dependent upon any items existing in the context previously
1 parent 7be572e commit e490683

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-10
lines changed

test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/DocumentTests/PagingTests.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,15 @@ public async Task Server_IncludesPagination_Links()
4848
{
4949
// arrange
5050
var pageSize = 5;
51-
var numberOfTodoItems = _context.TodoItems.Count();
52-
var numberOfPages = (int)Math.Ceiling(decimal.Divide(numberOfTodoItems, pageSize));
51+
const int minimumNumberOfRecords = 11;
52+
_context.TodoItems.RemoveRange(_context.TodoItems);
53+
54+
for(var i=0; i < minimumNumberOfRecords; i++)
55+
_context.TodoItems.Add(_todoItemFaker.Generate());
56+
57+
await _context.SaveChangesAsync();
58+
59+
var numberOfPages = (int)Math.Ceiling(decimal.Divide(minimumNumberOfRecords, pageSize));
5360
var startPageNumber = 2;
5461

5562
var builder = new WebHostBuilder()

test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/DocumentTests/Relationships.cs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
using JsonApiDotNetCore.Models;
1212
using JsonApiDotNetCoreExample.Data;
1313
using System.Linq;
14+
using Bogus;
15+
using JsonApiDotNetCoreExample.Models;
1416

1517
namespace 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

Comments
 (0)