I am using C# with .NET Core and Microsoft.VisualStudio.TestTools.UnitTesting.
When calling:
StringAssert.Contains(my_text, my_needle);
with some my_text values, for example:
StringAssert.Contains("foo bar baz", "hello");
a correct error message is displayed :
StringAssert.Contains failed. String 'foo bar baz' does not contain string 'hello'. .
at ...
However, if my_text has other values, the following error occurs:
Test method MyTest threw exception:
System.FormatException: Input string was not in a correct format.
at System.Text.ValueStringBuilder.ThrowFormatError()
at System.Text.ValueStringBuilder.AppendFormatHelper(IFormatProvider provider, String format, ParamsArray args)
at System.String.FormatHelper(IFormatProvider provider, String format, ParamsArray args)
at System.String.Format(IFormatProvider provider, String format, Object[] args)
at ...
Why is this happening?
Update: I have filed this as a bug report
A quick solution was to use:
StringAssert.Contains(my_text, my_needle, "", null);
Which works correctly!
Explanation: Using the debugger I have found
public static void Contains(string value, string substring)
Calls
public static void Contains(
string value,
string substring,
string message,
StringComparison comparisonType) {
// Calling:
StringAssert.Contains(value, substring, message, comparisonType, StringAssert.Empty);
Which calls:
public static void Contains(
string value,
string substring,
string message,
StringComparison comparisonType,
params object[] parameters)
{
... // calling:
Assert.HandleFail("StringAssert.Contains", finalMessage, parameters);
And inside HandleFail the value of parameters is object[] and:
parameters != null ? string.Format(...) : Assert.ReplaceNulls(message)
triggers a call to format (since parameters is not null...).
This seems like a bug.
To reproduce:
StringAssert.Contains("{", "x");
While this works OK:
StringAssert.Contains("{", "x", "", null);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With