1

I have a collection of strings

var greetings = new[] { "Hello boy", "Hello girl", "Hello all"};

I would like to create an assertion using FluentAssertions that verifies that the collection contains at least one item that contains a given string.

Example. For the previous collection,

  • testing with "boy", should pass.
  • testing with "Girl" should pass.
  • testing with "hello" should pass.
  • testing with "ouch" should NOT pass (because there is not item in the collection that contains this string).

I've tried with:

greetings.Should().ContainMatch("*" + str + "*");

But it doesn't work becuase it's case sensitive.

SuperJMN
  • 13,110
  • 16
  • 86
  • 185
  • `greetings.Where(g => g.Contains(str, StringComparison.InvariantCultureIgnoreCase)).Should().NotBeEmpty();` using the example provided by gunr – DavidG Aug 16 '21 at 14:40
  • For completeness, there should be a `ContainMatchEquivalentOf` (to mirror `String.MatchEquivalentOf`). Without it `greetings.Should().Contain(s => s.MatchEquivalentOf("*" + str + "*"))` should do it (disclaimer: not tested). – Jeroen Mostert Aug 16 '21 at 14:44
  • @JeroenMostert Sadly, your code doesn't work. The nested MatchEquivalentOf call isn't correct. Prepending "Should()" doesn't help, either. – SuperJMN Aug 16 '21 at 19:56
  • `greetings.Should().Contain(x=>x.Contains("Boy", StringComparison.InvariantCultureIgnoreCase));` – Matthias Sep 09 '21 at 23:00

0 Answers0