Skip to content

Commit

Permalink
added tests to Pings Controller
Browse files Browse the repository at this point in the history
  • Loading branch information
lkurzyniec committed May 8, 2024
1 parent 53da8e3 commit 3021d37
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 55 deletions.
@@ -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;
}
}
@@ -1,5 +1,7 @@
using HappyCode.NetCoreBoilerplate.Api.BackgroundServices;
using HappyCode.NetCoreBoilerplate.Api.Infrastructure.Filters;
using HappyCode.NetCoreBoilerplate.Api.IntegrationTests.Infrastructure.DataFeeders;
using HappyCode.NetCoreBoilerplate.Api.IntegrationTests.Infrastructure.Fakes;
using HappyCode.NetCoreBoilerplate.Core;
using HappyCode.NetCoreBoilerplate.Core.Registrations;
using Microsoft.EntityFrameworkCore;
Expand All @@ -26,7 +28,7 @@ public override void ConfigureServices(IServiceCollection services)
.AddDataAnnotations();

services.AddCoreComponents();
// services.AddTransient<ISomeService, SomeService>(); //if needed override registration with own test fakes
services.AddSingleton<IPingService, FakePingService>(); //override registration with own fakes

services.AddFeatureManagement();

Expand Down
@@ -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();
}
}
}
@@ -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();
}
}
}
@@ -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();
}
}
}

0 comments on commit 3021d37

Please sign in to comment.