aspnet/Testing

ExceptionAssert.ThrowsArgument* should force invariant culture

pranavkm opened this issue · 5 comments

Based on aspnet/Mvc#4260, it looks like we're verifying the exception message returned by ArgumentException without fixing the culture:

Stack Trace:
           … Xunit.Assert.Equal(String expected, String actual, Boolean ignoreCase, Boolean ignoreLineEndingDifferences, Boolean ignoreWhiteSpaceDifferences) dans C:\BuildAgent\work\cb37e9acf085d108\src\xunit.assert\Asserts\StringAsserts.cs:ligne 239
           … Microsoft.AspNetCore.Testing.ExceptionAssert.Throws[TException](Action testCode, String exceptionMessage)
           … Microsoft.AspNetCore.Testing.ExceptionAssert.ThrowsArgumentNullOrEmpty(Action testCode, String paramName)
           … Microsoft.AspNetCore.Mvc.Core.Test.ControllerBaseTest.Redirect_WithParameter_NullOrEmptyUrl_Throws(String url) dans C:\Users\Chalet K‚vin\Documents\GitHub\Forks\Mvc\test\Microsoft.AspNetCore.Mvc.Core.Test\ControllerBaseTest.cs:ligne 85
    Microsoft.AspNetCore.Mvc.Core.Test.ControllerBaseTest.Redirect_WithParameter_NullOrEmptyUrl_Throws(url: "") [FAIL]
      Assert.Equal() Failure
                                         � (pos 32)
      Expected: úúú be null or empty.\r\nParameter name: url
      Actual:   úúú be null or empty.\r\nNom du paramŠtreÿ: url

@pranavkm we also use Assert.Throws<T>() in a lot of our code. Since xUnit doesn't switch to a particular culture (see Record class), this looks like the wrong place for a "fix". Suggest instead using [ReplaceCulture] or using (new CultureReplacer()) blocks (occasionally) in the test themselves.

The unfortunate part is we construct the exception messages in the Assert.ThrowsArgument... method. We could skip that line entirely (split on new lines and such) which would be a saner thing to do. The user really has no expectation that we're generating non-localized strings here.

How does that change the need to fix the tests rather than this infrastructure piece which works (and should work) much like xUnit's methods?

Eilon commented

This doesn't sound right to me. What if we want the test to run in the local culture? I think the main problem is that we shouldn't be verifying the error messages of code we don't own.

Yup. We have a bunch of places where we do a partial match on the exception message. We just need to do something similar here i.e. not verify system messages.