-
-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathEmployeesTests.cs
106 lines (85 loc) · 3.42 KB
/
EmployeesTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using System.Net;
using FluentAssertions;
using HappyCode.NetCoreBoilerplate.Api.IntegrationTests.Extensions;
using HappyCode.NetCoreBoilerplate.Api.IntegrationTests.Infrastructure;
using HappyCode.NetCoreBoilerplate.Core.Dtos;
namespace HappyCode.NetCoreBoilerplate.Api.IntegrationTests
{
[Collection(nameof(TestServerClientCollection))]
public class EmployeesTests
{
private readonly HttpClient _client;
public EmployeesTests(TestServerClientFixture fixture)
{
_client = fixture.Client;
}
[Fact]
public async Task Get_should_return_NotFound_when_no_employee()
{
//when
var result = await _client.GetAsync($"api/employees/{int.MaxValue}");
//then
result.StatusCode.Should().Be(HttpStatusCode.NotFound);
}
[Fact]
public Task Get_should_return_Ok_with_expected_result()
{
//when
var result = _client.GetAsync("api/employees/1");
//then
return Verifier.Verify(result);
}
[Fact]
public async Task Delete_should_return_NoContent_when_delete_employee()
{
//when
var result = await _client.DeleteAsync("api/employees/99");
//then
result.StatusCode.Should().Be(HttpStatusCode.NoContent);
}
[Fact]
public async Task Delete_should_return_NotFound_when_no_employee()
{
//when
var result = await _client.DeleteAsync("api/employees/98765");
//then
result.StatusCode.Should().Be(HttpStatusCode.NotFound);
}
[Fact]
public async Task Put_should_return_NotFound_when_no_employee()
{
//given
var request = new EmployeePutDto { LastName = "Smith" };
//when
var result = await _client.PutAsync("api/employees/98765", request.ToStringContent());
//then
result.StatusCode.Should().Be(HttpStatusCode.NotFound);
}
[Fact]
public async Task Put_should_return_Ok_with_result_when_successfully_updated()
{
//given
var request = new EmployeePutDto { LastName = "Smith" };
//when
var result = await _client.PutAsync("api/employees/2", request.ToStringContent());
//then
result.EnsureSuccessStatusCode();
var emp = await result.Content.ReadAsJsonAsync<EmployeeDto>();
emp.LastName.Should().Be("Smith");
}
[Fact]
public async Task Post_should_return_Created_with_result_and_link_when_successfully_created()
{
//given
var request = new EmployeePostDto { FirstName = "Joann", LastName = "Richardson", Gender = "F", BirthDate = new DateTime(2003, 5, 1) };
//when
var result = await _client.PostAsync("api/employees/", request.ToStringContent());
//then
result.EnsureSuccessStatusCode();
var emp = await result.Content.ReadAsJsonAsync<EmployeeDto>();
emp.LastName.Should().Be("Richardson");
result.Headers.Location.ToString().Should().Contain("api/employees/100");
result.Headers.TryGetValues("x-date-created", out _).Should().BeTrue();
}
}
}