Skip to content

Commit

Permalink
test: Jobs.CancelAbandonedDonations => Adding unit test to make sure …
Browse files Browse the repository at this point in the history
…is not cancelling books which are not to be cancelled
  • Loading branch information
henriqueholtz committed Jun 14, 2024
1 parent 7184ba1 commit b6e9ac0
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using ShareBook.Domain.Common;
using System.Linq;
using System;
using Xunit.Extensions.Ordering;

namespace ShareBook.Test.Unit.Jobs
{
Expand All @@ -21,29 +22,47 @@ public class CancelAbandonedDonationsTests
private readonly Mock<IBookService> _mockBookService = new();
private readonly Mock<IBookUserService> _mockBookUserService = new();
private readonly Mock<IConfiguration> _mockConfiguration = new();

public CancelAbandonedDonationsTests()
{
// Mocking 4 books. 2 Of them are ready to be cancelled.
int maxLateDonationDaysAutoCancel = 90;
List<Book> allBooks = new List<Book> {
private static int _maxLateDonationDaysAutoCancel = 90;
private static List<Book> _allBooks = new List<Book> {
BookMock.GetLordTheRings(),
BookMock.GetLordTheRings(),
BookMock.GetLordTheRings(),
BookMock.GetLordTheRings(),
};
var booksToCancel = allBooks.Take(2);
foreach (var book in booksToCancel)
book.ChooseDate = DateTime.Now.AddDays((maxLateDonationDaysAutoCancel + 2) * -1);

_mockConfiguration.SetupGet(s => s[It.IsAny<string>()]).Returns(maxLateDonationDaysAutoCancel.ToString());
_mockBookService.Setup(s => s.GetBooksChooseDateIsLateAsync()).ReturnsAsync(allBooks);
public CancelAbandonedDonationsTests()
{

_mockConfiguration.SetupGet(s => s[It.IsAny<string>()]).Returns(_maxLateDonationDaysAutoCancel.ToString());
_mockBookService.Setup(s => s.GetBooksChooseDateIsLateAsync()).ReturnsAsync(_allBooks);
_mockBookUserService.Setup(s => s.CancelAsync(It.IsAny<BookCancelationDTO>())).ReturnsAsync(() => new Result<Book>(BookMock.GetLordTheRings()));
}

[Fact]
[Fact, Order(1)]
public async Task NotCancellingAnyBook()
{
CancelAbandonedDonations job = new CancelAbandonedDonations(_mockJobHistoryRepository.Object, _mockBookService.Object, _mockBookUserService.Object, _mockConfiguration.Object);

JobHistory result = await job.WorkAsync();

Assert.True(result.IsSuccess);
Assert.Equal("Encontradas 0 doações abandonadas com mais de 90 dias de atraso.\n\n", result.Details);
Assert.Equal(CancelAbandonedDonations.JobName, result.JobName);

_mockBookService.Verify(c => c.GetBooksChooseDateIsLateAsync(), Times.Once);
_mockBookUserService.Verify(c => c.CancelAsync(It.IsAny<BookCancelationDTO>()), Times.Never);
_mockBookService.VerifyNoOtherCalls();
_mockBookUserService.VerifyNoOtherCalls();
}

[Fact, Order(2)]
public async Task Cancelling2Of4AbandonedBooks()
{
// Mocking 4 books. 2 Of them are ready to be cancelled.
var booksToCancel = _allBooks.Take(2);
foreach (var book in booksToCancel)
book.ChooseDate = DateTime.Now.AddDays((_maxLateDonationDaysAutoCancel + 2) * -1);

CancelAbandonedDonations job = new CancelAbandonedDonations(_mockJobHistoryRepository.Object, _mockBookService.Object, _mockBookUserService.Object, _mockConfiguration.Object);

JobHistory result = await job.WorkAsync();
Expand Down
3 changes: 2 additions & 1 deletion ShareBook/ShareBook.Test.Unit/ShareBook.Test.Unit.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
Expand All @@ -11,6 +11,7 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="xunit" Version="2.7.1" />
<PackageReference Include="Xunit.Extensions.Ordering" Version="1.4.5" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.8">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down

0 comments on commit b6e9ac0

Please sign in to comment.