我正在努力寻找使用.TrueForAll
使用FluentAssertions的替代方案。注意,在Shoudly示例中,产品是List<Product>
,而在新代码中是IEnumerable<Product>
。
上面写着A constant value is expected
代表minPrice和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);
发布于 2022-10-15 10:25:13
https://fluentassertions.com/collections/
actual.Products.Should().OnlyContain(x => x.Price >= minPrice && x.Price <= maxPrice)
您所得到的错误来自于不正确地使用模式匹配-- is
后面的值需要是常量,而不是变量。
https://stackoverflow.com/questions/74078134
复制相似问题