|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Net; |
| 4 | +using System.Net.Http; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using Bogus; |
| 7 | +using DotNetCoreDocs; |
| 8 | +using DotNetCoreDocs.Writers; |
| 9 | +using JsonApiDotNetCore.Models; |
| 10 | +using JsonApiDotNetCore.Serialization; |
| 11 | +using JsonApiDotNetCore.Services; |
| 12 | +using JsonApiDotNetCoreExample; |
| 13 | +using JsonApiDotNetCoreExample.Data; |
| 14 | +using JsonApiDotNetCoreExample.Models; |
| 15 | +using Microsoft.AspNetCore.Hosting; |
| 16 | +using Microsoft.AspNetCore.TestHost; |
| 17 | +using Newtonsoft.Json; |
| 18 | +using Xunit; |
| 19 | +using Person = JsonApiDotNetCoreExample.Models.Person; |
| 20 | + |
| 21 | +namespace JsonApiDotNetCoreExampleTests.Acceptance.Spec |
| 22 | +{ |
| 23 | + [Collection("WebHostCollection")] |
| 24 | + public class FetchingRelationshipsTests |
| 25 | + { |
| 26 | + private DocsFixture<Startup, JsonDocWriter> _fixture; |
| 27 | + private IJsonApiContext _jsonApiContext; |
| 28 | + private Faker<TodoItem> _todoItemFaker; |
| 29 | + |
| 30 | + public FetchingRelationshipsTests(DocsFixture<Startup, JsonDocWriter> fixture) |
| 31 | + { |
| 32 | + _fixture = fixture; |
| 33 | + _jsonApiContext = fixture.GetService<IJsonApiContext>(); |
| 34 | + _todoItemFaker = new Faker<TodoItem>() |
| 35 | + .RuleFor(t => t.Description, f => f.Lorem.Sentence()) |
| 36 | + .RuleFor(t => t.Ordinal, f => f.Random.Number()); |
| 37 | + } |
| 38 | + |
| 39 | + [Fact] |
| 40 | + public async Task Request_UnsetRelationship_Returns_Null_DataObject() |
| 41 | + { |
| 42 | + // arrange |
| 43 | + var context = _fixture.GetService<AppDbContext>(); |
| 44 | + var todoItem = _todoItemFaker.Generate(); |
| 45 | + context.TodoItems.Add(todoItem); |
| 46 | + await context.SaveChangesAsync(); |
| 47 | + |
| 48 | + var builder = new WebHostBuilder() |
| 49 | + .UseStartup<Startup>(); |
| 50 | + |
| 51 | + var httpMethod = new HttpMethod("GET"); |
| 52 | + var route = $"/api/v1/todo-items/{todoItem.Id}/owner"; |
| 53 | + var server = new TestServer(builder); |
| 54 | + var client = server.CreateClient(); |
| 55 | + var request = new HttpRequestMessage(httpMethod, route); |
| 56 | + var expectedBody = "{\"data\":null}"; |
| 57 | + |
| 58 | + // act |
| 59 | + var response = await client.SendAsync(request); |
| 60 | + var body = await response.Content.ReadAsStringAsync(); |
| 61 | + |
| 62 | + // assert |
| 63 | + Assert.Equal(HttpStatusCode.OK, response.StatusCode); |
| 64 | + Assert.Equal("application/vnd.api+json", response.Content.Headers.ContentType.ToString()); |
| 65 | + Assert.Equal(expectedBody, body); |
| 66 | + |
| 67 | + context.Dispose(); |
| 68 | + } |
| 69 | + } |
| 70 | +} |
0 commit comments