The following is a failing unittest explaining a bug I found today:
[TestMethod]
public void WFT()
{
string configDebug = "false";
bool configDebugEnabled = bool.TryParse(configDebug, out configDebugEnabled);
Assert.AreEqual(false, configDebugEnabled);
}
This is how to make the test go from red to green:
[TestMethod]
public void WFT()
{
string configDebug = "false";
bool configDebugEnabled;
bool.TryParse(configDebug, out configDebugEnabled);
Assert.AreEqual(true, configDebugEnabled);
}
I haven’t been able to find the paragraph explaining this in the C# specification but there’s most likely a decent explanation to this behaviour. Can anybody explain to me why the first test is failing?
Because the
TryParsemethod always returnstrueif the parsing succeeds andfalseif not. In the first case the parsing succeeds soconfigDebugEnabled = truewhich is not what you assert.Btw the second test will also fail unless you write
string configDebug = "true".