For that purpose I would define extension method that would do correct assertion:
public static class TestUtils
{
public static void AssertContainsXTimes(this string actual, string expectedSubstring, int times)
{
var actualRemovedLength = actual.Length - actual.Replace(expectedSubstring, string.Empty).Length;
var expectedRemovedLength = expectedSubstring.Length * times;
Assert.Equal(expectedRemovedLength, actualRemovedLength);
}
public static void AssertContainsAtLeastXTimes(this string actual, string expectedSubstring, int times)
{
var actualRemovedLength = actual.Length - actual.Replace(expectedSubstring, string.Empty).Length;
var expectedRemovedLength = expectedSubstring.Length * times;
Assert.True(actualRemovedLength >= expectedRemovedLength);
}
}
And below is the screenshot of sample unit tests with their result for better understanding:
