0

I wanted to wrap the LINQ query in the AllSatisfy condition. How do I make it filter the products containing any of the keywords? I did it and it works, but I wonder how to wrap it in the AllSatisfy condition.

Should probably be smt similar to the one below:

actual.Products.Where(p => keywords.Any(k => p.Description.Contains(k)))
    .Should().AllSatisfy(p =>
    {
        p.Description.Should().MatchRegex(@"<\s*([^ >]+)[^>]*>.*?<\s*\/\s*\1\s*>");
    });

Unit test

[Theory]
[InlineData("blue")]
[InlineData("red", "blue")]
public async Task Handle_ShouldReturnFilteredHighlightedDescriptions_WhenGivenKeyword(params string[] keywords)
{
    // Arrange
    var query = new GetProductsQuery(Highlight: keywords);

    // Act
    var actual = await _queryHandler.Handle(query, default);

    // Assert

    // A list of products whose description contains any of the keywords in a string array
    var list = (from product in actual.Products
            from keyword in keywords
            where product.Description.Contains(keyword)
            select product)
        .ToList();

    list.Should().AllSatisfy(p =>
    {
        p.Description.Should().MatchRegex(@"<\s*([^ >]+)[^>]*>.*?<\s*\/\s*\1\s*>");
    });
}
nop
  • 4,711
  • 6
  • 32
  • 93

0 Answers0