Search code examples
c#unit-testingmoqassert

what is the difference between expected and actual values in Assert.Matches()


The static Matches is

public static void Matches(string expectedRegexPattern, string actualString);

What is the difference between when I pass expectedRegexPattern value instead of actualString value and passing actualString value instead of expectedRegexPattern value?

Way 1 - Assert.Matches("EN", result[0].LanguageCode);

Way 2 - Assert.Matches(result[0].LanguageCode,"EN");

Both of ways doing same work with same performance. So am confused about the difference between the above ways and which one is best?


Solution

  • @Stivi correctly points out a significant difference in the case of Matches, but there's another less significant but still important distinction even for Equals and other assertion methods.

    You might get an incorrect message in the test log output. Many testing frameworks will log an error such as

    Values do not match: expected: {expectedValue}, actual: {actualValue}".

    So if you switched them, you'd see the wrong "expected" value, potentially confusing someone trying to diagnose a problem.

    Take this test as an example:

    void TestValueIsZero():
    {
        int value = 1;
        Assert.Equals(0, value);
        // logs "Values to not match. expected: 0, actual: 1
        Assert.Equals(value, 0);
        // logs "Values to not match. expected: 1, actual: 0
    }
    

    Someone looking at the failed test logs might be confused by the incorrect "expected" and "actual" values in the log due to the switched parameters.

    The mechanics may be exactly the same regardless of the order of parameters (compare the two objects and return true if no differences are found), but the semantics are still important.