Skip to content

Commit 8b443d8

Browse files
author
Bart Koelman
committed
Refactored tests for exception handling in serializer
1 parent ddcd4ac commit 8b443d8

File tree

8 files changed

+71
-96
lines changed

8 files changed

+71
-96
lines changed

src/Examples/JsonApiDotNetCoreExample/Controllers/ThrowingResourcesController.cs

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

src/Examples/JsonApiDotNetCoreExample/Data/AppDbContext.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ public AppDbContext(DbContextOptions<AppDbContext> options, ISystemClock systemC
2828

2929
protected override void OnModelCreating(ModelBuilder modelBuilder)
3030
{
31-
modelBuilder.Entity<ThrowingResource>();
32-
3331
modelBuilder.Entity<SuperUser>().HasBaseType<User>();
3432

3533
modelBuilder.Entity<TodoItem>()

src/Examples/JsonApiDotNetCoreExample/Models/ThrowingResource.cs

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

test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/ThrowingResourceTests.cs

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

test/JsonApiDotNetCoreExampleTests/IntegrationTests/ExceptionHandling/ErrorDbContext.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ namespace JsonApiDotNetCoreExampleTests.IntegrationTests.ExceptionHandling
55
public sealed class ErrorDbContext : DbContext
66
{
77
public DbSet<ConsumerArticle> ConsumerArticles { get; set; }
8+
public DbSet<ThrowingArticle> ThrowingArticles { get; set; }
89

910
public ErrorDbContext(DbContextOptions<ErrorDbContext> options)
1011
: base(options)

test/JsonApiDotNetCoreExampleTests/IntegrationTests/ExceptionHandling/ExceptionHandlerTests.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using JsonApiDotNetCore.Serialization.Objects;
88
using Microsoft.Extensions.DependencyInjection;
99
using Microsoft.Extensions.Logging;
10+
using Newtonsoft.Json.Linq;
1011
using Xunit;
1112

1213
namespace JsonApiDotNetCoreExampleTests.IntegrationTests.ExceptionHandling
@@ -79,7 +80,46 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
7980
responseDocument.Errors[0].Meta.Data["support"].Should().Be("Please contact us for info about similar articles at company@email.com.");
8081

8182
loggerFactory.Logger.Messages.Should().HaveCount(1);
83+
loggerFactory.Logger.Messages.Single().LogLevel.Should().Be(LogLevel.Warning);
8284
loggerFactory.Logger.Messages.Single().Text.Should().Contain("Article with code 'X123' is no longer available.");
8385
}
86+
87+
[Fact]
88+
public async Task Logs_and_produces_error_response_on_serialization_failure()
89+
{
90+
// Arrange
91+
var loggerFactory = _testContext.Factory.Services.GetRequiredService<FakeLoggerFactory>();
92+
loggerFactory.Logger.Clear();
93+
94+
var throwingArticle = new ThrowingArticle();
95+
96+
await _testContext.RunOnDatabaseAsync(async dbContext =>
97+
{
98+
dbContext.ThrowingArticles.Add(throwingArticle);
99+
await dbContext.SaveChangesAsync();
100+
});
101+
102+
var route = "/throwingArticles/" + throwingArticle.StringId;
103+
104+
// Act
105+
var (httpResponse, responseDocument) = await _testContext.ExecuteGetAsync<ErrorDocument>(route);
106+
107+
// Assert
108+
httpResponse.Should().HaveStatusCode(HttpStatusCode.InternalServerError);
109+
110+
responseDocument.Errors.Should().HaveCount(1);
111+
responseDocument.Errors[0].StatusCode.Should().Be(HttpStatusCode.InternalServerError);
112+
responseDocument.Errors[0].Title.Should().Be("An unhandled error occurred while processing this request.");
113+
responseDocument.Errors[0].Detail.Should().Be("Exception has been thrown by the target of an invocation.");
114+
115+
var stackTraceLines =
116+
((JArray) responseDocument.Errors[0].Meta.Data["stackTrace"]).Select(token => token.Value<string>());
117+
118+
stackTraceLines.Should().ContainMatch("* System.InvalidOperationException: Article status could not be determined.*");
119+
120+
loggerFactory.Logger.Messages.Should().HaveCount(1);
121+
loggerFactory.Logger.Messages.Single().LogLevel.Should().Be(LogLevel.Error);
122+
loggerFactory.Logger.Messages.Single().Text.Should().Contain("Exception has been thrown by the target of an invocation.");
123+
}
84124
}
85125
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.ComponentModel.DataAnnotations.Schema;
3+
using JsonApiDotNetCore.Resources;
4+
using JsonApiDotNetCore.Resources.Annotations;
5+
6+
namespace JsonApiDotNetCoreExampleTests.IntegrationTests.ExceptionHandling
7+
{
8+
public sealed class ThrowingArticle : Identifiable
9+
{
10+
[Attr]
11+
[NotMapped]
12+
public string Status => throw new InvalidOperationException("Article status could not be determined.");
13+
}
14+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using JsonApiDotNetCore.Configuration;
2+
using JsonApiDotNetCore.Controllers;
3+
using JsonApiDotNetCore.Services;
4+
using Microsoft.Extensions.Logging;
5+
6+
namespace JsonApiDotNetCoreExampleTests.IntegrationTests.ExceptionHandling
7+
{
8+
public sealed class ThrowingArticlesController : JsonApiController<ThrowingArticle>
9+
{
10+
public ThrowingArticlesController(IJsonApiOptions options, ILoggerFactory loggerFactory,
11+
IResourceService<ThrowingArticle> resourceService)
12+
: base(options, loggerFactory, resourceService)
13+
{
14+
}
15+
}
16+
}

0 commit comments

Comments
 (0)