|
| 1 | +using DotNetCoreDocs; |
| 2 | +using JsonApiDotNetCoreExample; |
| 3 | +using DotNetCoreDocs.Writers; |
| 4 | +using Newtonsoft.Json; |
| 5 | +using JsonApiDotNetCore.Internal; |
| 6 | +using JsonApiDotNetCore.Serialization; |
| 7 | +using Xunit; |
| 8 | +using System.Diagnostics; |
| 9 | + |
| 10 | +namespace JsonApiDotNetCoreExampleTests.Acceptance.Extensibility |
| 11 | +{ |
| 12 | + public class CustomErrorTests |
| 13 | + { |
| 14 | + [Fact] |
| 15 | + public void Can_Return_Custom_Error_Types() |
| 16 | + { |
| 17 | + // while(!Debugger.IsAttached) { bool stop = false; } |
| 18 | + |
| 19 | + // arrange |
| 20 | + var error = new CustomError("507", "title", "detail", "custom"); |
| 21 | + var errorCollection = new ErrorCollection(); |
| 22 | + errorCollection.Add(error); |
| 23 | + |
| 24 | + var expectedJson = JsonConvert.SerializeObject(new { |
| 25 | + errors = new dynamic[] { |
| 26 | + new { |
| 27 | + myCustomProperty = "custom", |
| 28 | + title = "title", |
| 29 | + detail = "detail", |
| 30 | + status = "507" |
| 31 | + } |
| 32 | + } |
| 33 | + }); |
| 34 | + |
| 35 | + // act |
| 36 | + var result = new JsonApiSerializer(null, null, null) |
| 37 | + .Serialize(errorCollection); |
| 38 | + |
| 39 | + // assert |
| 40 | + Assert.Equal(expectedJson, result); |
| 41 | + |
| 42 | + } |
| 43 | + |
| 44 | + class CustomError : Error { |
| 45 | + public CustomError(string status, string title, string detail, string myProp) |
| 46 | + : base(status, title, detail) |
| 47 | + { |
| 48 | + MyCustomProperty = myProp; |
| 49 | + } |
| 50 | + public string MyCustomProperty { get; set; } |
| 51 | + } |
| 52 | + } |
| 53 | +} |
0 commit comments