unit tests and finishing touches

This commit is contained in:
fox
2026-06-24 14:18:37 +01:00
parent dd0c586a1f
commit e395a186b7
7 changed files with 247 additions and 3 deletions
+1 -1
View File
@@ -9,6 +9,6 @@
<Project Path="libs/Solicitors.HtmlParsing/Solicitors.HtmlParsing.csproj" />
</Folder>
<Folder Name="/tests/">
<Project Path="tests/Solicitors.HtmlParsing.Tests/Solicitors.HtmlParsing.Tests.csproj" />
<Project Path="tests/Solicitors.Core.Tests/Solicitors.Core.Tests.csproj" />
</Folder>
</Solution>
@@ -7,6 +7,7 @@
</PropertyGroup>
<ItemGroup>
<InternalsVisibleTo Include="Solicitors.Core.Tests" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="10.0.9" />
</ItemGroup>
@@ -0,0 +1,130 @@
using Moq;
using Solicitors.Core.Data;
using Solicitors.Core.Models;
namespace Solicitors.Core.Tests;
public class SolicitorServiceTests
{
private readonly Mock<IReadOnlySolicitorRepository> _mockRepo;
private readonly SolicitorService _service;
public SolicitorServiceTests()
{
_mockRepo = new Mock<IReadOnlySolicitorRepository>();
_service = new SolicitorService(_mockRepo.Object);
}
[Fact]
public async Task GetSolicitorInfoByIdAsync_Should_ReturnNull_When_NoMatchingSolicitor()
{
//arrange
var cts = new CancellationTokenSource();
var id = Guid.NewGuid();
_mockRepo
.Setup(x => x.GetSolicitorByIdAsync(It.IsAny<Guid>(), cts.Token))
.ReturnsAsync(default(Solicitor?));
//act
var result = await _service.GetSolicitorInfoByIdAsync(id, cts.Token);
//assert
Assert.Null(result);
_mockRepo.Verify(x => x.GetSolicitorByIdAsync(id, cts.Token), Times.Once);
}
[Fact]
public async Task GetSolicitorInfoByIdAsync_Should_ReturnMainSolicitorInfo_When_MatchingSolicitor()
{
//arrange
var cts = new CancellationTokenSource();
var id = Guid.NewGuid();
var expected = new Solicitor
{
SolicitorId = id,
Name = "Solicitor",
ShortDescription = "Short Desc",
Phone = "000 0000 0000",
Email = "example@example.com",
Website = "example.com",
RelativeUrl = "/-some-solicitor.html"
};
_mockRepo
.Setup(x => x.GetSolicitorByIdAsync(It.IsAny<Guid>(), cts.Token))
.ReturnsAsync(expected);
//act
var result = await _service.GetSolicitorInfoByIdAsync(id, cts.Token);
//assert
Assert.NotNull(result);
Assert.Equal(result.Id, expected.SolicitorId);
Assert.Equal(result.Name, expected.Name);
Assert.Equal(result.ShortDescription, expected.ShortDescription);
Assert.Equal(result.Phone, expected.Phone);
Assert.Equal(result.Email, expected.Email);
Assert.Equal(result.Website, expected.Website);
}
[Fact]
public async Task GetSolicitorInfoByIdAsync_Should_MapCities_When_CitiesArePresent()
{
//arrange
var cts = new CancellationTokenSource();
var id = Guid.NewGuid();
var expected = new Solicitor
{
Name = "Solicitor",
RelativeUrl = "/-some-solicitor.html"
};
expected.Cities.Add(new City { Name = "London" });
_mockRepo
.Setup(x => x.GetSolicitorByIdAsync(It.IsAny<Guid>(), cts.Token))
.ReturnsAsync(expected);
// expected.Ratings.Add(new SolicitorRating { Provider = "Google", Value = 4, Maximum = 5, Solicitor = expected });
//act
var result = await _service.GetSolicitorInfoByIdAsync(id, cts.Token);
//assert
Assert.NotNull(result);
Assert.Collection(result.Cities, city => Assert.Equal("London", city));
}
[Fact]
public async Task GetSolicitorInfoByIdAsync_Should_MapRatings_When_RatingsArePresent()
{
//arrange
var cts = new CancellationTokenSource();
var id = Guid.NewGuid();
var expected = new Solicitor
{
Name = "Solicitor",
RelativeUrl = "/-some-solicitor.html"
};
expected.Ratings.Add(new SolicitorRating { Provider = "Google", Value = 4, Maximum = 5, Solicitor = expected });
_mockRepo
.Setup(x => x.GetSolicitorByIdAsync(It.IsAny<Guid>(), cts.Token))
.ReturnsAsync(expected);
//act
var result = await _service.GetSolicitorInfoByIdAsync(id, cts.Token);
//assert
Assert.NotNull(result);
Assert.Collection(result.Ratings, rating =>
{
Assert.Equal("Google", rating.Provider);
Assert.Equal(4, rating.Value);
Assert.Equal(5, rating.Maximum);
});
}
}
@@ -10,6 +10,7 @@
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.4" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.4" />
</ItemGroup>
@@ -19,7 +20,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\libs\Solicitors.HtmlParsing\Solicitors.HtmlParsing.csproj" />
<ProjectReference Include="..\..\libs\Solicitors.Core\Solicitors.Core.csproj" />
</ItemGroup>
</Project>