I'm trying to assert that a list contains a certain string. Since I'd need the condition to be evaluated case insensitively, I used a workaround (something along this blog post).
However, I'd like to know why there seems not to be a way to make the Assert.Contains method perform the comparison without regard of case sensitivity. Or is there a way to do that? (When I googled it, I only got hits on constraints for the Assert.That method on the official page for nUnit.)
There is no way to specify ignoreCase in the Assert.Contains
. Whether it's something that is overlooked or intended I do not know. You can, however, use
StringAssert.AreEqualIgnoringCase(left, right);
in your unit tests to achieve the same results.
Alternatively, if you wish to stick with the Assert.Foo()
"theme", you could do something like this:
Assert.IsTrue(string.Equals(left, right, StringComparison.OrdinalIgnoreCase));
or, since Contains
treats arrays:
Assert.IsTrue(list.Any(element => element.ToUpper() == "VILTERSTEN"));
where you call ToUpper()
on both the left and right string operands, which effectively makes the comparison ignore case as well. OrdinalIgnoreCase
is to ensure some corner cases (read: Turkish) of cultures do not cause unexpected results. If you are interested in reading up on that, have a look at the Turkey test.