我正在嘗試尋找.TrueForAll使用 FluentAssertions 的替代方法。請注意,在 Shoudly 示例中 Products 是List<Product>,而在新代碼中是IEnumerable<Product>.
它對A constant value is expectedminPrice 和 maxPrice 說:
actual.Products.All(x => x.Price is >= minPrice and <= maxPrice).Should().BeTrue(); // A constant value is expected
// or
actual.Products.Should().OnlyContain(x => x.Price is >= minPrice and <= maxPrice); // A constant value is expected
應該(舊代碼)
[Theory]
[InlineData(10)]
[InlineData(12)]
[InlineData(5)]
public async Task Handle_ReturnsFilteredProductByMaxPrice(double maxPrice)
{
var parameters = new ProductParams() { MaxPrice = maxPrice };
var result = await _handler.Handle(new GetProductsQuery(parameters), CancellationToken.None);
result.Value.Products.TrueForAll(x => x.Price <= maxPrice);
}
FluentAssertions(新代碼)
[Theory]
[InlineData(10, 14)]
[InlineData(22, 24)]
public async Task Handle_ShouldReturnFilteredSubsetOfProducts_WhenGivenMinPriceAndMaxPrice(double minPrice, double maxPrice)
{
// Arrange
var query = new GetProductsQuery(MinPrice: minPrice, MaxPrice: maxPrice);
// Act
var actual = await _queryHandler.Handle(query, default);
// Assert
actual.Products.All(x => x.Price is >= minPrice and <= maxPrice).Should().BeTrue();
}
public record Product(
[property: JsonPropertyName("title")] string Title,
[property: JsonPropertyName("price")] double Price,
[property: JsonPropertyName("sizes")] List<string> Sizes,
[property: JsonPropertyName("description")] string Description);
uj5u.com熱心網友回復:
https://fluentassertions.com/collections/
actual.Products.Should().OnlyContain(x => x.Price >= minPrice && x.Price <= maxPrice)
您得到的錯誤來自不正確地使用模式匹配 - 之后的值is需要是常量,而不是變數。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/514879.html
