[Fact]
public async Task UpdateAsync_ReturnsUpdatedOrderItem_WhenOrderItemExists()
{
// Arrange
var orderItems = new List<OrderItem>
{
new OrderItem {Id = "1", Title = "Item 1", Description = "Description 1", Price = 9.99f, Quantity = 2},
new OrderItem {Id = "2", Title = "Item 2", Description = "Description 2", Price = 4.99f, Quantity = 1}
};
var mockDbContext = new Mock<ApplicationDbContext>(new DbContextOptions<ApplicationDbContext>());
var mockDbSet = orderItems.AsQueryable().BuildMockDbSet();
mockDbSet.Setup(x => x.AnyAsync(It.IsAny<Expression<Func<OrderItem, bool>>>(), It.IsAny<CancellationToken>()))
.ReturnsAsync((Expression<Func<OrderItem, bool>> predicate, CancellationToken cancellationToken)
=> orderItems.AsQueryable().Any(predicate.Compile()));
mockDbContext.Setup(x => x.OrderItems).Returns(mockDbSet.Object);
var updatedOrderItem = new OrderItem
{
Id = "2",
Title = "Updated Item 2",
Description = "Updated Description 2",
Price = 19.99f,
Quantity = 3
};
// Act
var result = await _orderItemRepository.UpdateAsync(updatedOrderItem);
// Assert
Assert.NotNull(result);
Assert.Equal(updatedOrderItem, result);
mockDbSet.Verify(x => x.Update(updatedOrderItem), Times.Once());
mockDbContext.Verify(x => x.SaveChangesAsync(default), Times.Once());
}