Skip to content

Commit

Permalink
switched to FluentAssertions from Shouldly - FluentAssertions has bet…
Browse files Browse the repository at this point in the history
…ter API
  • Loading branch information
lkurzyniec committed Apr 8, 2020
1 parent bd82d16 commit 609e4bc
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using FluentAssertions;
using HappyCode.NetCoreBoilerplate.Api.IntegrationTests.Infrastructure;
using HappyCode.NetCoreBoilerplate.Core.Dtos;
using Shouldly;
using Xunit;

namespace HappyCode.NetCoreBoilerplate.Api.IntegrationTests
Expand All @@ -24,9 +24,9 @@ public async Task Get_should_return_Ok_with_results()
{
var result = await _client.GetAsync($"api/cars");

result.StatusCode.ShouldBe(HttpStatusCode.OK);
result.StatusCode.Should().Be(HttpStatusCode.OK);
var cars = await result.Content.ReadAsJsonAsync<List<CarDto>>();
cars.Count.ShouldBeGreaterThan(0);
cars.Count.Should().BeGreaterThan(0);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using FluentAssertions;
using HappyCode.NetCoreBoilerplate.Api.IntegrationTests.Infrastructure;
using HappyCode.NetCoreBoilerplate.Core.Dtos;
using Shouldly;
using Xunit;

namespace HappyCode.NetCoreBoilerplate.Api.IntegrationTests
Expand All @@ -23,33 +23,33 @@ public async Task Get_should_return_NotFound_when_no_employee()
{
var result = await _client.GetAsync($"api/employees/123456");

result.StatusCode.ShouldBe(HttpStatusCode.NotFound);
result.StatusCode.Should().Be(HttpStatusCode.NotFound);
}

[Fact]
public async Task Get_should_return_Ok_with_expected_result()
{
var result = await _client.GetAsync($"api/employees/1");

result.StatusCode.ShouldBe(HttpStatusCode.OK);
result.StatusCode.Should().Be(HttpStatusCode.OK);
var emp = await result.Content.ReadAsJsonAsync<EmployeeDto>();
emp.LastName.ShouldBe("Anderson");
emp.LastName.Should().Be("Anderson");
}

[Fact]
public async Task Delete_should_return_NoContent_when_delete_employee()
{
var result = await _client.DeleteAsync($"api/employees/99");

result.StatusCode.ShouldBe(HttpStatusCode.NoContent);
result.StatusCode.Should().Be(HttpStatusCode.NoContent);
}

[Fact]
public async Task Delete_should_return_NotFound_when_no_employee()
{
var result = await _client.DeleteAsync($"api/employees/98765");

result.StatusCode.ShouldBe(HttpStatusCode.NotFound);
result.StatusCode.Should().Be(HttpStatusCode.NotFound);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.10.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="3.1.3" />
<PackageReference Include="Shouldly" Version="3.0.2" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
<PrivateAssets>all</PrivateAssets>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
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 Shouldly;
using Xunit;

namespace HappyCode.NetCoreBoilerplate.Api.UnitTests.Controllers
Expand Down Expand Up @@ -47,11 +47,11 @@ public async Task Get_should_return_Ok_with_expected_result()
var result = await Controller.Get(default) as OkObjectResult;

//then
result.ShouldNotBeNull();
result.StatusCode.ShouldBe(StatusCodes.Status200OK);
result.Value.ShouldBeAssignableTo<IEnumerable<CarDto>>();
result.Should().NotBeNull();
result.StatusCode.Should().Be(StatusCodes.Status200OK);
result.Value.Should().BeAssignableTo<IEnumerable<CarDto>>();
var cars = result.Value as IEnumerable<CarDto>;
cars.Count().ShouldBe(2);
cars.Should().HaveCount(2);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using System.Threading;
using System.Threading.Tasks;
using FluentAssertions;
using HappyCode.NetCoreBoilerplate.Api.Controllers;
using HappyCode.NetCoreBoilerplate.Core.Dtos;
using HappyCode.NetCoreBoilerplate.Core.Repositories;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Moq;
using Shouldly;
using Xunit;

namespace HappyCode.NetCoreBoilerplate.Api.UnitTests.Controllers
Expand Down Expand Up @@ -44,8 +44,8 @@ public async Task Get_should_return_NotFound_when_repository_return_null()
var result = await Controller.Get(1, default) as StatusCodeResult;

//then
result.ShouldNotBeNull();
result.StatusCode.ShouldBe(StatusCodes.Status404NotFound);
result.Should().NotBeNull();
result.StatusCode.Should().Be(StatusCodes.Status404NotFound);
}

[Fact]
Expand All @@ -66,12 +66,14 @@ public async Task Get_should_return_Ok_with_expected_result_when_repository_retu
var result = await Controller.Get(1, default) as OkObjectResult;

//then
result.ShouldNotBeNull();
result.StatusCode.ShouldBe(StatusCodes.Status200OK);
result.Value.ShouldBeAssignableTo<EmployeeDto>();
result.Should().NotBeNull();
result.StatusCode.Should().Be(StatusCodes.Status200OK);
result.Value.Should().BeAssignableTo<EmployeeDto>();
var emp = result.Value as EmployeeDto;
emp.Id.ShouldBe(empId);
emp.LastName.ShouldBe(lastName);
emp.Id.Should().Be(empId);
emp.LastName.Should()
.NotBeNullOrEmpty()
.And.Be(lastName);
}

[Fact]
Expand All @@ -98,8 +100,8 @@ public async Task Delete_should_return_NotFound_when_repository_return_false()
var result = await Controller.Delete(1, default) as StatusCodeResult;

//then
result.ShouldNotBeNull();
result.StatusCode.ShouldBe(StatusCodes.Status404NotFound);
result.Should().NotBeNull();
result.StatusCode.Should().Be(StatusCodes.Status404NotFound);
}

[Fact]
Expand All @@ -113,8 +115,8 @@ public async Task Delete_should_return_NoContent_when_repository_return_true()
var result = await Controller.Delete(1, default) as StatusCodeResult;

//then
result.ShouldNotBeNull();
result.StatusCode.ShouldBe(StatusCodes.Status204NoContent);
result.Should().NotBeNull();
result.StatusCode.Should().Be(StatusCodes.Status204NoContent);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="FluentAssertions" Version="5.10.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="Moq" Version="4.13.1" />
<PackageReference Include="Moq.AutoMocker.NETStandard" Version="0.5.0.9" />
<PackageReference Include="Shouldly" Version="3.0.2" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="FluentAssertions" Version="5.10.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="MockQueryable.Moq" Version="3.1.1" />
<PackageReference Include="Moq" Version="4.13.1" />
<PackageReference Include="Moq.AutoMocker.NETStandard" Version="0.5.0.9" />
<PackageReference Include="Shouldly" Version="3.0.2" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using FluentAssertions;
using HappyCode.NetCoreBoilerplate.Core.Models;
using HappyCode.NetCoreBoilerplate.Core.Services;
using HappyCode.NetCoreBoilerplate.Core.UnitTests.Infrastructure;
using Microsoft.EntityFrameworkCore;
using Moq;
using Shouldly;
using Xunit;

namespace HappyCode.NetCoreBoilerplate.Core.UnitTests.Repositories
Expand Down Expand Up @@ -39,7 +39,7 @@ public async Task GetOldestAsync_should_return_expected_employee()
var result = await _service.GetAllSortedByPlateAsync(default);

//then
result.First().Id.ShouldBe(2);
result.First().Id.Should().Be(2);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using FluentAssertions;
using HappyCode.NetCoreBoilerplate.Core.Models;
using HappyCode.NetCoreBoilerplate.Core.Repositories;
using HappyCode.NetCoreBoilerplate.Core.UnitTests.Infrastructure;
using Microsoft.EntityFrameworkCore;
using Moq;
using Shouldly;
using Xunit;

namespace HappyCode.NetCoreBoilerplate.Core.UnitTests.Repositories
Expand Down Expand Up @@ -41,8 +41,10 @@ public async Task GetOldestAsync_should_return_expected_employee()
var emp = await _repository.GetOldestAsync(default);

//then
emp.Id.ShouldBe(45);
emp.LastName.ShouldBe("Hudson");
emp.Id.Should().Be(45);
emp.LastName.Should()
.NotBeNullOrEmpty()

This comment has been minimized.

Copy link
@nZeus

nZeus Apr 9, 2020

There is no need to do .NotBeNullOrEmpty().
.Should().Be("Hudson") throws the well-descriptive message anyway.

This comment has been minimized.

Copy link
@lkurzyniec

lkurzyniec Apr 10, 2020

Author Owner

@nZeus I know, I just want to show that there is And for chaining ;)

.And.Be("Hudson");
}

[Fact]
Expand All @@ -55,7 +57,7 @@ public async Task DeleteByIdAsync_should_return_false_when_employee_not_found()
var result = await _repository.DeleteByIdAsync(99, default);

//then
result.ShouldBe(false);
result.Should().Be(false);

_dbContextMock.Verify(x => x.SaveChangesAsync(It.IsAny<CancellationToken>()), Times.Never);
}
Expand All @@ -79,7 +81,7 @@ public async Task DeleteByIdAsync_should_return_true_and_save_when_employee_foun
var result = await _repository.DeleteByIdAsync(empId, default);

//then
result.ShouldBe(true);
result.Should().Be(true);

_dbContextMock.Verify(x => x.Employees.Remove(It.Is<Employee>(y => y.EmpNo == empId)), Times.Once);
_dbContextMock.Verify(x => x.SaveChangesAsync(It.IsAny<CancellationToken>()), Times.Once);
Expand Down

0 comments on commit 609e4bc

Please sign in to comment.