|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Linq; |
| 3 | +using System.Net; |
| 4 | +using System.Net.Http; |
| 5 | +using System.Net.Http.Headers; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using Bogus; |
| 8 | +using DotNetCoreDocs; |
| 9 | +using DotNetCoreDocs.Writers; |
| 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 | + |
| 20 | +namespace JsonApiDotNetCoreExampleTests.Acceptance.Spec |
| 21 | +{ |
| 22 | + [Collection("WebHostCollection")] |
| 23 | + public class DeletingDataTests |
| 24 | + { |
| 25 | + private DocsFixture<Startup, JsonDocWriter> _fixture; |
| 26 | + private AppDbContext _context; |
| 27 | + private Faker<TodoItem> _todoItemFaker; |
| 28 | + |
| 29 | + public DeletingDataTests(DocsFixture<Startup, JsonDocWriter> fixture) |
| 30 | + { |
| 31 | + _fixture = fixture; |
| 32 | + _context = fixture.GetService<AppDbContext>(); |
| 33 | + _todoItemFaker = new Faker<TodoItem>() |
| 34 | + .RuleFor(t => t.Description, f => f.Lorem.Sentence()) |
| 35 | + .RuleFor(t => t.Ordinal, f => f.Random.Number()); |
| 36 | + } |
| 37 | + |
| 38 | + [Fact] |
| 39 | + public async Task Respond_404_If_EntityDoesNotExist() |
| 40 | + { |
| 41 | + // arrange |
| 42 | + var maxPersonId = _context.TodoItems.LastOrDefault()?.Id ?? 0; |
| 43 | + var todoItem = _todoItemFaker.Generate(); |
| 44 | + var builder = new WebHostBuilder() |
| 45 | + .UseStartup<Startup>(); |
| 46 | + |
| 47 | + var server = new TestServer(builder); |
| 48 | + var client = server.CreateClient(); |
| 49 | + |
| 50 | + var httpMethod = new HttpMethod("DELETE"); |
| 51 | + var route = $"/api/v1/todo-items/{maxPersonId + 100}"; |
| 52 | + var request = new HttpRequestMessage(httpMethod, route); |
| 53 | + |
| 54 | + // Act |
| 55 | + var response = await client.SendAsync(request); |
| 56 | + |
| 57 | + // Assert |
| 58 | + Assert.Equal(HttpStatusCode.NotFound, response.StatusCode); |
| 59 | + } |
| 60 | + } |
| 61 | +} |
0 commit comments