首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >FluentAssertions替代TrueForAll

FluentAssertions替代TrueForAll
EN

Stack Overflow用户
提问于 2022-10-15 09:11:58
回答 1查看 40关注 0票数 0

我正在努力寻找使用.TrueForAll使用FluentAssertions的替代方案。注意,在Shoudly示例中,产品是List<Product>,而在新代码中是IEnumerable<Product>

上面写着A constant value is expected代表minPrice和maxPrice:

代码语言:javascript
运行
复制
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

(旧代码)

代码语言:javascript
运行
复制
[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 (新代码)

代码语言:javascript
运行
复制
[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();
}
代码语言:javascript
运行
复制
public record Product(
    [property: JsonPropertyName("title")] string Title,
    [property: JsonPropertyName("price")] double Price,
    [property: JsonPropertyName("sizes")] List<string> Sizes,
    [property: JsonPropertyName("description")] string Description);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-10-15 10:25:13

https://fluentassertions.com/collections/

代码语言:javascript
运行
复制
actual.Products.Should().OnlyContain(x => x.Price >= minPrice && x.Price <= maxPrice)

您所得到的错误来自于不正确地使用模式匹配-- is后面的值需要是常量,而不是变量。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74078134

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档