Skip to content

Commit

Permalink
refactor: Update repository update calls to use SaveChangesAsync()
Browse files Browse the repository at this point in the history
  • Loading branch information
foxminchan committed Aug 30, 2024
1 parent 6a458eb commit bd657ef
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public async Task GivenRequest_ShouldDeleteBook_WhenBookExists()
x => x.GetByIdAsync(It.IsAny<Guid>(), It.IsAny<CancellationToken>()),
Times.Once);
_bookRepositoryMock.Verify(
x => x.UpdateAsync(It.IsAny<Book>(), It.IsAny<CancellationToken>()),
x => x.SaveChangesAsync(It.IsAny<CancellationToken>()),
Times.Once);
}

Expand All @@ -63,7 +63,7 @@ public async Task GivenRequest_ShouldReturnNotFound_WhenBookDoesNotExist()
x => x.GetByIdAsync(It.IsAny<Guid>(), It.IsAny<CancellationToken>()),
Times.Once);
_bookRepositoryMock.Verify(
x => x.UpdateAsync(It.IsAny<Book>(), It.IsAny<CancellationToken>()),
x => x.SaveChangesAsync(It.IsAny<CancellationToken>()),
Times.Never);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ public async Task GivenValidRemoveBookImageCommand_ShouldRemoveBookImageAndRetur
_azuriteMock.Setup(a => a.DeleteFileAsync(book.ImageUrl!, It.IsAny<CancellationToken>()))
.Returns(Task.CompletedTask);

_repositoryMock.Setup(r => r.UpdateAsync(book, It.IsAny<CancellationToken>()))
.Returns(Task.CompletedTask);

var command = new RemoveBookImageCommand(bookId);

// Act
Expand Down Expand Up @@ -61,7 +58,7 @@ public async Task GivenNonExistentBookId_ShouldThrowNotFoundException()

_repositoryMock.Verify(r => r.GetByIdAsync(bookId, It.IsAny<CancellationToken>()), Times.Once);
_azuriteMock.Verify(a => a.DeleteFileAsync(It.IsAny<string>(), It.IsAny<CancellationToken>()), Times.Never);
_repositoryMock.Verify(r => r.UpdateAsync(It.IsAny<Book>(), It.IsAny<CancellationToken>()), Times.Never);
_repositoryMock.Verify(r => r.SaveChangesAsync(It.IsAny<CancellationToken>()), Times.Never);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public async Task GivenValidUpdateBookCommand_ShouldReturnSuccess()
// Assert
result.IsSuccess.Should().BeTrue();
_repositoryMock.Verify(r => r.GetByIdAsync(bookId, It.IsAny<CancellationToken>()), Times.Once);
_repositoryMock.Verify(r => r.UpdateAsync(book, It.IsAny<CancellationToken>()), Times.Once);
_repositoryMock.Verify(r => r.SaveChangesAsync(It.IsAny<CancellationToken>()), Times.Once);

book.Name.Should().Be("Updated Name");
book.Description.Should().Be("Updated Description");
Expand Down Expand Up @@ -73,7 +73,7 @@ public async Task GivenNonExistentBookId_ShouldThrowNotFoundException()
await act.Should().ThrowAsync<NotFoundException>();

_repositoryMock.Verify(r => r.GetByIdAsync(bookId, It.IsAny<CancellationToken>()), Times.Once);
_repositoryMock.Verify(r => r.UpdateAsync(It.IsAny<Book>(), It.IsAny<CancellationToken>()), Times.Never);
_repositoryMock.Verify(r => r.SaveChangesAsync(It.IsAny<CancellationToken>()), Times.Never);
}

[Theory]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public async Task GivenValidOrderId_ShouldCancelOrderAndPublishEvent()
// Assert
result.IsSuccess.Should().BeTrue();
order.Status.Should().Be(Status.Canceled);
_repository.Verify(x => x.UpdateAsync(order, It.IsAny<CancellationToken>()), Times.Once);
_repository.Verify(x => x.SaveChangesAsync(It.IsAny<CancellationToken>()), Times.Once);
_publishEndpoint.Verify(
x => x.Publish(It.IsAny<OrderCancelledIntegrationEvent>(), It.IsAny<CancellationToken>()), Times.Once);
}
Expand All @@ -50,7 +50,7 @@ public async Task GivenInvalidOrderId_ShouldThrowNotFoundException_WhenOrderNotF

// Assert
await act.Should().ThrowAsync<NotFoundException>();
_repository.Verify(x => x.UpdateAsync(It.IsAny<Order>(), It.IsAny<CancellationToken>()), Times.Never);
_repository.Verify(x => x.SaveChangesAsync(It.IsAny<CancellationToken>()), Times.Never);
_publishEndpoint.Verify(
x => x.Publish(It.IsAny<OrderCancelledIntegrationEvent>(), It.IsAny<CancellationToken>()), Times.Never);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public async Task GivenValidOrderId_ShouldCompleteOrderAndPublishEvent()
// Assert
result.IsSuccess.Should().BeTrue();
order.Status.Should().Be(Status.Completed);
_repository.Verify(x => x.UpdateAsync(order, It.IsAny<CancellationToken>()), Times.Once);
_repository.Verify(x => x.SaveChangesAsync(It.IsAny<CancellationToken>()), Times.Once);
_publishEndpoint.Verify(
x => x.Publish(It.IsAny<OrderCompletedIntegrationEvent>(), It.IsAny<CancellationToken>()), Times.Once);
}
Expand All @@ -50,7 +50,7 @@ public async Task GivenInvalidOrderId_ShouldThrowNotFoundException_WhenOrderNotF

// Assert
await act.Should().ThrowAsync<NotFoundException>();
_repository.Verify(x => x.UpdateAsync(It.IsAny<Order>(), It.IsAny<CancellationToken>()), Times.Never);
_repository.Verify(x => x.SaveChangesAsync(It.IsAny<CancellationToken>()), Times.Never);
_publishEndpoint.Verify(
x => x.Publish(It.IsAny<OrderCompletedIntegrationEvent>(), It.IsAny<CancellationToken>()), Times.Never);
}
Expand Down

0 comments on commit bd657ef

Please sign in to comment.