Edit on GitHub

xUnit2009 Warning

Do not use boolean check to check for substrings

Cause

A violation of this rule occurs when Assert.True or Assert.False are used to check for substrings with string methods like string.Contains, string.StartsWith and string.EndsWith.

Reason for rule

There are specialized assertions for substring checks.

How to fix violations

To fix a violation of this rule, replace the offending assertion according to this:

Examples

Violates

[Fact]
public void ExampleMethod()
{
    string result = "foo bar baz";

    Assert.True(result.Contains("bar"));
    Assert.True(result.StartsWith("foo"));
}

Does not violate

[Fact]
public void ExampleMethod()
{
    string result = "foo bar baz";

    Assert.Contains("bar", result);
    Assert.StartsWith("foo", result);
}

How to suppress violations

#pragma warning disable xUnit2009 // Do not use boolean check to check for substrings
#pragma warning restore xUnit2009 // Do not use boolean check to check for substrings
Copyright © 2017 .NET Foundation. Contributions welcomed at https://github.com/xunit/xunit.analyzers.