How can make a matcher for a string containing a substring, while ignoring case

Issue

I’m writing a unit test using mockito to mock a dependency and check we’re calling it with the right argument. We should be passing in a string, so I’m trying to match on that function argument, but without asserting on the whole string, in case we change the wording. So I want to match on just one word in the message, but that word could be at the start of the sentence or in the middle, so it might start with a capital.

The dart matchers have equalsIgnoringCase, and contains, but I can’t find any matcher that handles both, something like containsIgnoringCase. Is there any way to check for the substring, while also ignoring case in a matcher?

Solution

You can pass in a regex to the contains method with the caseSensitive property set to false.

string.contains(new RegExp(r'your-substring', caseSensitive: false));

Answered By – Ajmal

Answer Checked By – Senaida (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *