-
-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
53da8e3
commit 3021d37
Showing
5 changed files
with
150 additions
and
55 deletions.
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
...HappyCode.NetCoreBoilerplate.Api.IntegrationTests/Infrastructure/Fakes/FakePingService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using System.Net; | ||
using HappyCode.NetCoreBoilerplate.Api.BackgroundServices; | ||
|
||
namespace HappyCode.NetCoreBoilerplate.Api.IntegrationTests.Infrastructure.Fakes | ||
{ | ||
public class FakePingService : IPingService | ||
{ | ||
internal const HttpStatusCode Result = HttpStatusCode.EarlyHints; | ||
|
||
public HttpStatusCode WebsiteStatusCode => Result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
test/HappyCode.NetCoreBoilerplate.Api.IntegrationTests/PingsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using FluentAssertions; | ||
using HappyCode.NetCoreBoilerplate.Api.IntegrationTests.Infrastructure; | ||
using HappyCode.NetCoreBoilerplate.Api.IntegrationTests.Infrastructure.Fakes; | ||
|
||
namespace HappyCode.NetCoreBoilerplate.Api.IntegrationTests | ||
{ | ||
[Collection(nameof(TestServerClientCollection))] | ||
public class PingsTests | ||
{ | ||
private readonly HttpClient _client; | ||
|
||
public PingsTests(TestServerClientFixture fixture) | ||
{ | ||
_client = fixture.Client; | ||
} | ||
|
||
[Fact] | ||
public async Task Get_website_should_return_Ok_with_result() | ||
{ | ||
//when | ||
var result = await _client.GetAsync("api/pings/website"); | ||
|
||
//then | ||
result.EnsureSuccessStatusCode(); | ||
var status = await result.Content.ReadAsStringAsync(); | ||
status.Should().Contain(FakePingService.Result.ToString()); | ||
} | ||
|
||
[Fact] | ||
public async Task Get_random_should_return_Ok_with_not_empty_result() | ||
{ | ||
//when | ||
var result = await _client.GetAsync("api/pings/random"); | ||
|
||
//then | ||
result.EnsureSuccessStatusCode(); | ||
var status = await result.Content.ReadAsStringAsync(); | ||
status.Should().NotBeNullOrEmpty(); | ||
} | ||
} | ||
} |
100 changes: 46 additions & 54 deletions
100
test/HappyCode.NetCoreBoilerplate.Api.UnitTests/Controllers/CarsControllerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,46 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using AutoFixture.Xunit2; | ||
using FluentAssertions; | ||
using HappyCode.NetCoreBoilerplate.Api.Controllers; | ||
using HappyCode.NetCoreBoilerplate.Core.Dtos; | ||
using HappyCode.NetCoreBoilerplate.Core.Services; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Moq; | ||
using Xunit; | ||
|
||
namespace HappyCode.NetCoreBoilerplate.Api.UnitTests.Controllers | ||
{ | ||
public class CarsControllerTests : ControllerTestsBase<CarsController> | ||
{ | ||
private readonly Mock<ICarService> _carServiceMock; | ||
|
||
public CarsControllerTests() | ||
{ | ||
_carServiceMock = Mocker.GetMock<ICarService>(); | ||
} | ||
|
||
[Fact] | ||
public async Task GetAll_should_call_GetAllSortedByPlateAsync_onto_service() | ||
{ | ||
//when | ||
await Controller.GetAllAsync(default); | ||
|
||
//then | ||
_carServiceMock.Verify(x => x.GetAllSortedByPlateAsync(It.IsAny<CancellationToken>()), Times.Once); | ||
} | ||
|
||
[Theory, AutoData] | ||
public async Task GetAll_should_return_Ok_with_expected_result(IEnumerable<CarDto> cars) | ||
{ | ||
//given | ||
_carServiceMock.Setup(x => x.GetAllSortedByPlateAsync(It.IsAny<CancellationToken>())) | ||
.ReturnsAsync(cars); | ||
|
||
//when | ||
var result = await Controller.GetAllAsync(default) as OkObjectResult; | ||
|
||
//then | ||
result.Should().NotBeNull(); | ||
result.StatusCode.Should().Be(StatusCodes.Status200OK); | ||
result.Value.Should().BeAssignableTo<IEnumerable<CarDto>>(); | ||
var value = result.Value as IEnumerable<CarDto>; | ||
value.Should().HaveCount(cars.Count()); | ||
} | ||
} | ||
} | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using AutoFixture.Xunit2; | ||
using FluentAssertions; | ||
using HappyCode.NetCoreBoilerplate.Api.Controllers; | ||
using HappyCode.NetCoreBoilerplate.Core.Dtos; | ||
using HappyCode.NetCoreBoilerplate.Core.Services; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Moq; | ||
using Xunit; | ||
|
||
namespace HappyCode.NetCoreBoilerplate.Api.UnitTests.Controllers | ||
{ | ||
public class CarsControllerTests : ControllerTestsBase<CarsController> | ||
{ | ||
private readonly Mock<ICarService> _carServiceMock; | ||
|
||
public CarsControllerTests() | ||
{ | ||
_carServiceMock = Mocker.GetMock<ICarService>(); | ||
} | ||
|
||
[Theory, AutoData] | ||
public async Task GetAll_should_return_Ok_with_expected_result(IEnumerable<CarDto> cars) | ||
{ | ||
//given | ||
_carServiceMock.Setup(x => x.GetAllSortedByPlateAsync(It.IsAny<CancellationToken>())) | ||
.ReturnsAsync(cars); | ||
|
||
//when | ||
var result = await Controller.GetAllAsync(default) as OkObjectResult; | ||
|
||
//then | ||
result.Should().NotBeNull(); | ||
result.StatusCode.Should().Be(StatusCodes.Status200OK); | ||
result.Value.Should().BeAssignableTo<IEnumerable<CarDto>>(); | ||
var value = result.Value as IEnumerable<CarDto>; | ||
value.Should().HaveCount(cars.Count()); | ||
|
||
_carServiceMock.VerifyAll(); | ||
} | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
test/HappyCode.NetCoreBoilerplate.Api.UnitTests/Controllers/PingsControllerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using System.Net; | ||
using AutoFixture.Xunit2; | ||
using FluentAssertions; | ||
using HappyCode.NetCoreBoilerplate.Api.BackgroundServices; | ||
using HappyCode.NetCoreBoilerplate.Api.Controllers; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Xunit; | ||
|
||
namespace HappyCode.NetCoreBoilerplate.Api.UnitTests.Controllers | ||
{ | ||
public class PingsControllerTests : ControllerTestsBase<PingsController> | ||
{ | ||
[Theory, AutoData] | ||
public void GetWebsitePingStatusCode_should_return_Ok_with_expected_result(HttpStatusCode code) | ||
{ | ||
//given | ||
var pingServiceMock = Mocker.GetMock<IPingService>(); | ||
pingServiceMock.SetupGet(x => x.WebsiteStatusCode) | ||
.Returns(code); | ||
|
||
//when | ||
var result = Controller.GetWebsitePingStatusCode() as OkObjectResult; | ||
|
||
//then | ||
result.Should().NotBeNull(); | ||
result.StatusCode.Should().Be(StatusCodes.Status200OK); | ||
result.Value.Should().BeAssignableTo<string>(); | ||
var value = result.Value as string; | ||
value.Should().Contain(code.ToString()); | ||
value.Should().Contain(((int)code).ToString()); | ||
} | ||
|
||
[Fact] | ||
public void GetRandomStatusCode_should_return_Ok_with_expected_result() | ||
{ | ||
//when | ||
var result = Controller.GetRandomStatusCode() as OkObjectResult; | ||
|
||
//then | ||
result.Should().NotBeNull(); | ||
result.StatusCode.Should().Be(StatusCodes.Status200OK); | ||
result.Value.Should().BeAssignableTo<string>(); | ||
var value = result.Value as string; | ||
value.Should().NotBeNullOrEmpty(); | ||
} | ||
} | ||
} |