Search code examples
c#.netunit-testingxunitfluent-assertions

Regex.IsMatch to MatchRegex in FluentAssertions


I looked at the documentation and there were methods e.g.Match, MatchRegex, etc., so I decided to improve the commented statement below and replace Regex.IsMatch with MatchRegex. However, I'm getting a compile-time error. How do I fix it?

It's a List<Product> that shouldn't contain anything that could be matched to the following regex: @"<\s*([^ >]+)[^>]*>.*?<\s*/\s*\1\s*>".

//actual.Products.Should().NotContain(p => Regex.IsMatch(p.Description, @"<\s*([^ >]+)[^>]*>.*?<\s*/\s*\1\s*>"));

actual.Products.Should().NotContain(p =>
    p.Description.Should().MatchRegex(@"<\s*([^ >]+)[^>]*>.*?<\s*/\s*\1\s*>")); // compile-time error

Solution

  • You could try this:

      actual.Products.Should().AllSatisfy(p =>
            p.Description.Should().NotMatchRegex(@"<\s*([^ >]+)[^>]*>.*?<\s*/\s*\1\s*>"));