Skip to content

Commit 22738b6

Browse files
author
Bart Koelman
committed
Refactored tests for ActionResult usage
1 parent 44653f8 commit 22738b6

File tree

6 files changed

+242
-227
lines changed

6 files changed

+242
-227
lines changed

src/Examples/JsonApiDotNetCoreExample/Controllers/TodoItemsTestController.cs

Lines changed: 0 additions & 110 deletions
This file was deleted.

test/JsonApiDotNetCoreExampleTests/Acceptance/ActionResultTests.cs

Lines changed: 0 additions & 117 deletions
This file was deleted.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using Microsoft.EntityFrameworkCore;
2+
3+
namespace JsonApiDotNetCoreExampleTests.IntegrationTests.ControllerActionResults
4+
{
5+
public sealed class ActionResultDbContext : DbContext
6+
{
7+
public DbSet<Toothbrush> Toothbrushes { get; set; }
8+
9+
public ActionResultDbContext(DbContextOptions<ActionResultDbContext> options)
10+
: base(options)
11+
{
12+
}
13+
}
14+
}
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
using System.Net;
2+
using System.Threading.Tasks;
3+
using FluentAssertions;
4+
using JsonApiDotNetCore.Serialization.Objects;
5+
using Xunit;
6+
7+
namespace JsonApiDotNetCoreExampleTests.IntegrationTests.ControllerActionResults
8+
{
9+
public sealed class ActionResultTests
10+
: IClassFixture<IntegrationTestContext<TestableStartup<ActionResultDbContext>, ActionResultDbContext>>
11+
{
12+
private readonly IntegrationTestContext<TestableStartup<ActionResultDbContext>, ActionResultDbContext> _testContext;
13+
14+
public ActionResultTests(IntegrationTestContext<TestableStartup<ActionResultDbContext>, ActionResultDbContext> testContext)
15+
{
16+
_testContext = testContext;
17+
}
18+
19+
[Fact]
20+
public async Task Can_get_resource_by_ID()
21+
{
22+
// Arrange
23+
var toothbrush = new Toothbrush();
24+
25+
await _testContext.RunOnDatabaseAsync(async dbContext =>
26+
{
27+
dbContext.Toothbrushes.Add(toothbrush);
28+
await dbContext.SaveChangesAsync();
29+
});
30+
31+
var route = "/toothbrushes/" + toothbrush.StringId;
32+
33+
// Act
34+
var (httpResponse, responseDocument) = await _testContext.ExecuteGetAsync<Document>(route);
35+
36+
// Assert
37+
httpResponse.Should().HaveStatusCode(HttpStatusCode.OK);
38+
39+
responseDocument.SingleData.Should().NotBeNull();
40+
responseDocument.SingleData.Id.Should().Be(toothbrush.StringId);
41+
}
42+
43+
[Fact]
44+
public async Task Converts_empty_ActionResult_to_error_collection()
45+
{
46+
// Arrange
47+
var route = "/toothbrushes/11111111";
48+
49+
// Act
50+
var (httpResponse, responseDocument) = await _testContext.ExecuteGetAsync<ErrorDocument>(route);
51+
52+
// Assert
53+
httpResponse.Should().HaveStatusCode(HttpStatusCode.NotFound);
54+
55+
responseDocument.Errors.Should().HaveCount(1);
56+
responseDocument.Errors[0].StatusCode.Should().Be(HttpStatusCode.NotFound);
57+
responseDocument.Errors[0].Title.Should().Be("NotFound");
58+
responseDocument.Errors[0].Detail.Should().BeNull();
59+
}
60+
61+
[Fact]
62+
public async Task Converts_ActionResult_with_error_object_to_error_collection()
63+
{
64+
// Arrange
65+
var route = "/toothbrushes/22222222";
66+
67+
// Act
68+
var (httpResponse, responseDocument) = await _testContext.ExecuteGetAsync<ErrorDocument>(route);
69+
70+
// Assert
71+
httpResponse.Should().HaveStatusCode(HttpStatusCode.NotFound);
72+
73+
responseDocument.Errors.Should().HaveCount(1);
74+
responseDocument.Errors[0].StatusCode.Should().Be(HttpStatusCode.NotFound);
75+
responseDocument.Errors[0].Title.Should().Be("No toothbrush with that ID exists.");
76+
responseDocument.Errors[0].Detail.Should().BeNull();
77+
}
78+
79+
[Fact]
80+
public async Task Cannot_convert_ActionResult_with_string_parameter_to_error_collection()
81+
{
82+
// Arrange
83+
var route = "/toothbrushes/33333333";
84+
85+
// Act
86+
var (httpResponse, responseDocument) = await _testContext.ExecuteGetAsync<ErrorDocument>(route);
87+
88+
// Assert
89+
httpResponse.Should().HaveStatusCode(HttpStatusCode.InternalServerError);
90+
91+
responseDocument.Errors.Should().HaveCount(1);
92+
responseDocument.Errors[0].StatusCode.Should().Be(HttpStatusCode.InternalServerError);
93+
responseDocument.Errors[0].Title.Should().Be("An unhandled error occurred while processing this request.");
94+
responseDocument.Errors[0].Detail.Should().Be("Data being returned must be errors or resources.");
95+
}
96+
97+
[Fact]
98+
public async Task Converts_ObjectResult_with_error_object_to_error_collection()
99+
{
100+
// Arrange
101+
var route = "/toothbrushes/44444444";
102+
103+
// Act
104+
var (httpResponse, responseDocument) = await _testContext.ExecuteGetAsync<ErrorDocument>(route);
105+
106+
// Assert
107+
httpResponse.Should().HaveStatusCode(HttpStatusCode.BadGateway);
108+
109+
responseDocument.Errors.Should().HaveCount(1);
110+
responseDocument.Errors[0].StatusCode.Should().Be(HttpStatusCode.BadGateway);
111+
responseDocument.Errors[0].Title.Should().BeNull();
112+
responseDocument.Errors[0].Detail.Should().BeNull();
113+
}
114+
115+
[Fact]
116+
public async Task Converts_ObjectResult_with_error_objects_to_error_collection()
117+
{
118+
// Arrange
119+
var route = "/toothbrushes/55555555";
120+
121+
// Act
122+
var (httpResponse, responseDocument) = await _testContext.ExecuteGetAsync<ErrorDocument>(route);
123+
124+
// Assert
125+
httpResponse.Should().HaveStatusCode(HttpStatusCode.BadRequest);
126+
127+
responseDocument.Errors.Should().HaveCount(3);
128+
129+
responseDocument.Errors[0].StatusCode.Should().Be(HttpStatusCode.PreconditionFailed);
130+
responseDocument.Errors[0].Title.Should().BeNull();
131+
responseDocument.Errors[0].Detail.Should().BeNull();
132+
133+
responseDocument.Errors[1].StatusCode.Should().Be(HttpStatusCode.Unauthorized);
134+
responseDocument.Errors[1].Title.Should().BeNull();
135+
responseDocument.Errors[1].Detail.Should().BeNull();
136+
137+
responseDocument.Errors[2].StatusCode.Should().Be(HttpStatusCode.ExpectationFailed);
138+
responseDocument.Errors[2].Title.Should().Be("This is not a very great request.");
139+
responseDocument.Errors[2].Detail.Should().BeNull();
140+
}
141+
}
142+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using JsonApiDotNetCore.Resources;
2+
3+
namespace JsonApiDotNetCoreExampleTests.IntegrationTests.ControllerActionResults
4+
{
5+
public sealed class Toothbrush : Identifiable
6+
{
7+
}
8+
}

0 commit comments

Comments
 (0)