Skip to content

Commit b93b9f7

Browse files
committed
test(fetching): Request for an empty set should return an empty set
Issue #21 - 1
1 parent 3c74bec commit b93b9f7

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Net;
4+
using System.Net.Http;
5+
using System.Threading.Tasks;
6+
using DotNetCoreDocs;
7+
using DotNetCoreDocs.Writers;
8+
using JsonApiDotNetCore.Serialization;
9+
using JsonApiDotNetCore.Services;
10+
using JsonApiDotNetCoreExample;
11+
using JsonApiDotNetCoreExample.Data;
12+
using JsonApiDotNetCoreExample.Models;
13+
using Microsoft.AspNetCore.Hosting;
14+
using Microsoft.AspNetCore.TestHost;
15+
using Newtonsoft.Json;
16+
using Xunit;
17+
18+
namespace JsonApiDotNetCoreExampleTests.Acceptance.Spec
19+
{
20+
[Collection("WebHostCollection")]
21+
public class FetchingDataTests
22+
{
23+
private DocsFixture<Startup, JsonDocWriter> _fixture;
24+
private IJsonApiContext _jsonApiContext;
25+
26+
public FetchingDataTests(DocsFixture<Startup, JsonDocWriter> fixture)
27+
{
28+
_fixture = fixture;
29+
_jsonApiContext = fixture.GetService<IJsonApiContext>();
30+
}
31+
32+
[Fact]
33+
public async Task Request_ForEmptyCollection_Returns_EmptyDataCollection()
34+
{
35+
// arrange
36+
var context = _fixture.GetService<AppDbContext>();
37+
context.TodoItems.RemoveRange(context.TodoItems);
38+
await context.SaveChangesAsync();
39+
40+
var builder = new WebHostBuilder()
41+
.UseStartup<Startup>();
42+
var httpMethod = new HttpMethod("GET");
43+
var route = "/api/v1/todo-items";
44+
var server = new TestServer(builder);
45+
var client = server.CreateClient();
46+
var request = new HttpRequestMessage(httpMethod, route);
47+
var expectedBody = JsonConvert.SerializeObject(new {
48+
data = new List<object>()
49+
});
50+
51+
// act
52+
var response = await client.SendAsync(request);
53+
var body = await response.Content.ReadAsStringAsync();
54+
var deserializedBody = JsonApiDeSerializer.DeserializeList<TodoItem>(body, _jsonApiContext);
55+
56+
// assert
57+
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
58+
Assert.Equal("application/vnd.api+json", response.Content.Headers.ContentType.ToString());
59+
Assert.Empty(deserializedBody);
60+
Assert.Equal(expectedBody, body);
61+
62+
context.Dispose();
63+
}
64+
}
65+
}

0 commit comments

Comments
 (0)