When to pass Culture when comparing strings using Assert.AreEqual () in C #

I came across some unit tests written by another developer who regularly use the overloaded version of Assert.AreEqual as follows:

  Assert.AreEqual(stringparamX, stringParamY, true, CultureInfo.InvariantCulture); 

stringParamX set within the unit test, and stringparamY will be the result of the system under test.

Perhaps this code can be ported to other countries, and these tests can be performed. However, looking at the MSDN docs for Culture , I cannot help but think that passing to CultureInfo.InvariantCulture adds unnecessary complexity here. I'm not sure what remote deletion will have on tests if they are performed in other cultures.

In the context of unit testing in my situation, why should I (or not) write such statements?

+5
source share
2 answers

There is no reason to use this overload if you specify the culture as InvariantCulture because this is the default value.

From the documentation here :

An invariant culture is used for comparison.

Please note that there are some cases where this would make a difference if you did not use the InvariantCulture , the most notorious Turkish Problem I.

The following code illustrates the problem:

 Thread.CurrentThread.CurrentCulture = new CultureInfo("tr-TR"); Assert.AreEqual("i", "I", true, CultureInfo.CurrentCulture); // This throws. Assert.AreEqual("i", "I", true, CultureInfo.InvariantCulture); // This doesn't throw. Assert.AreEqual("i", "I", true); // This doesn't throw. 

However, note that this will not affect you because you are using an invariant culture.

+3
source

The MSDN documentation for Assert.AreEqual, which does not accept the CultureInfo state in the Remarks section:

Invariant culture used for comparison

If true, then specifying it is clearly not technically unnecessary. This is then a moot point about whether it is better to be explicit.

Thus, it depends on what is being tested, whether culture should be considered for case-insensitive string comparisons.

+2
source

Source: https://habr.com/ru/post/1207653/


All Articles