forked from lkurzyniec/netcore-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EmployeesControllerTests.cs
219 lines (178 loc) · 7.6 KB
/
EmployeesControllerTests.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using AutoFixture.Xunit2;
using FluentAssertions;
using HappyCode.NetCoreBoilerplate.Api.Controllers;
using HappyCode.NetCoreBoilerplate.Core;
using HappyCode.NetCoreBoilerplate.Core.Dtos;
using HappyCode.NetCoreBoilerplate.Core.Repositories;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.FeatureManagement;
using Moq;
using Xunit;
namespace HappyCode.NetCoreBoilerplate.Api.UnitTests.Controllers
{
public class EmployeesControllerTests : ControllerTestsBase<EmployeesController>
{
private readonly Mock<IEmployeeRepository> _employeeRepositoryMock;
public EmployeesControllerTests()
{
_employeeRepositoryMock = Mocker.GetMock<IEmployeeRepository>();
}
[Theory, AutoData]
public async Task GetAll_should_return_expected_results(List<EmployeeDto> employees)
{
//given
_employeeRepositoryMock.Setup(x => x.GetAllAsync(It.IsAny<CancellationToken>()))
.ReturnsAsync(employees);
//when
var result = await Controller.GetAll(default) as OkObjectResult;
//then
result.Should().NotBeNull();
result.Value.Should().BeAssignableTo<IEnumerable<EmployeeDto>>()
.And.BeEquivalentTo(employees);
_employeeRepositoryMock.Verify(x => x.GetAllAsync(It.IsAny<CancellationToken>()), Times.Once);
}
[Fact]
public async Task Get_should_call_GetByIdAsync_onto_repository()
{
//given
const int empId = 11;
//when
await Controller.Get(11, default);
//then
_employeeRepositoryMock.Verify(x => x.GetByIdAsync(empId, It.IsAny<CancellationToken>()), Times.Once);
}
[Fact]
public async Task Get_should_return_NotFound_when_repository_return_null()
{
//given
_employeeRepositoryMock.Setup(x => x.GetByIdAsync(It.IsAny<int>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(() => null);
//when
var result = await Controller.Get(1, default) as StatusCodeResult;
//then
result.Should().NotBeNull();
result.StatusCode.Should().Be(StatusCodes.Status404NotFound);
}
[Fact]
public async Task Get_should_return_Ok_with_expected_result_when_repository_return_object()
{
//given
const int empId = 22;
const string lastName = "Smith";
_employeeRepositoryMock.Setup(x => x.GetByIdAsync(It.IsAny<int>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new EmployeeDto
{
Id = empId,
LastName = lastName,
});
//when
var result = await Controller.Get(1, default) as OkObjectResult;
//then
result.Should().NotBeNull();
result.StatusCode.Should().Be(StatusCodes.Status200OK);
result.Value.Should().BeAssignableTo<EmployeeDto>();
var emp = result.Value as EmployeeDto;
emp.Id.Should().Be(empId);
emp.LastName.Should()
.NotBeNullOrEmpty()
.And.Be(lastName);
}
[Fact]
public async Task Delete_should_call_DeleteByIdAsync_onto_repository()
{
//given
const int empId = 11;
//when
await Controller.Delete(11, default);
//then
_employeeRepositoryMock.Verify(x => x.DeleteByIdAsync(empId, It.IsAny<CancellationToken>()), Times.Once);
}
[Fact]
public async Task Delete_should_return_NotFound_when_repository_return_false()
{
//given
_employeeRepositoryMock.Setup(x => x.DeleteByIdAsync(It.IsAny<int>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(false);
//when
var result = await Controller.Delete(1, default) as StatusCodeResult;
//then
result.Should().NotBeNull();
result.StatusCode.Should().Be(StatusCodes.Status404NotFound);
}
[Fact]
public async Task Delete_should_return_NoContent_when_repository_return_true()
{
//given
_employeeRepositoryMock.Setup(x => x.DeleteByIdAsync(It.IsAny<int>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(true);
//when
var result = await Controller.Delete(1, default) as StatusCodeResult;
//then
result.Should().NotBeNull();
result.StatusCode.Should().Be(StatusCodes.Status204NoContent);
}
[Theory, AutoData]
public async Task Put_should_return_NotFound_when_repository_return_null(int empId, EmployeePutDto employeePutDto)
{
//given
_employeeRepositoryMock.Setup(x => x.UpdateAsync(empId, employeePutDto, It.IsAny<CancellationToken>()))
.ReturnsAsync(() => null)
.Verifiable();
//when
var result = await Controller.Put(empId, employeePutDto, default) as StatusCodeResult;
//then
result.StatusCode.Should().Be(StatusCodes.Status404NotFound);
_employeeRepositoryMock.Verify();
}
[Theory, AutoData]
public async Task Put_should_return_Ok_with_result_when_update_finished_with_success(int empId, EmployeePutDto employeePutDto, EmployeeDto employee)
{
//given
_employeeRepositoryMock.Setup(x => x.UpdateAsync(empId, employeePutDto, It.IsAny<CancellationToken>()))
.ReturnsAsync(employee)
.Verifiable();
//when
var result = await Controller.Put(empId, employeePutDto, default) as OkObjectResult;
//then
result.Value.Should().BeAssignableTo<EmployeeDto>()
.And.BeEquivalentTo(employee);
_employeeRepositoryMock.Verify();
}
[Theory, AutoData]
public async Task Post_should_return_Created_with_result_and_header_when_insert_finished_with_success(EmployeePostDto employeePostDto, EmployeeDto employee)
{
//given
_employeeRepositoryMock.Setup(x => x.InsertAsync(employeePostDto, It.IsAny<CancellationToken>()))
.ReturnsAsync(employee)
.Verifiable();
//when
var result = await Controller.Post(employeePostDto, default) as ObjectResult;
//then
result.StatusCode.Should().Be(StatusCodes.Status201Created);
result.Value.Should().BeAssignableTo<EmployeeDto>()
.And.BeEquivalentTo(employee);
Controller.HttpContext.Response.Headers.TryGetValue("x-date-created", out _).Should().BeTrue();
_employeeRepositoryMock.Verify();
}
[Fact]
public async Task GetOldest_should_return_Santa_when_feature_enabled()
{
//given
var featureManagerMock = Mocker.GetMock<IFeatureManager>();
featureManagerMock.Setup(x => x.IsEnabledAsync(FeatureFlags.Santa))
.ReturnsAsync(true)
.Verifiable();
//when
var result = await Controller.GetOldest(default) as ObjectResult;
//then
result.Should().NotBeNull();
result.StatusCode.Should().Be(StatusCodes.Status200OK);
result.Value.Should().BeAssignableTo<EmployeeDto>()
.Which.FirstName.Should().Be("Santa");
}
}
}