Casting to int should be fine:
Assert.AreEqual(0, (int)Command.None);
質問
I have the following test for an enum:
[TestCase]
public void NoneIsDefaultTest()
{
Assert.AreEqual(0, Command.None);
}
The idea is to ensure that no additions to the enum change the default value. However, the test fails with:
Expected: 0
But was: None
Is Assert.AreEqual automatically applying .ToString()? How can I avoid this?
Edit: enum definition:
internal enum Command { None = 0, Build, Config, Reconfig, Help, Version }
解決 2
Casting to int should be fine:
Assert.AreEqual(0, (int)Command.None);
他のヒント
AreEqual
is not using ToString
for the comparison, only when formatting the error message. Enums form a type distinct from the underlying value type, so the comparison 0.Equals(Command.None)
returns false, since the enum value is not an int.
Casting to an int works, but if all you want to test is the default value (which is what was behind my question), you can use the default keyword:
[TestCase]
public void NoneIsDefaultTest()
{
Assert.AreEqual(default(Command), Command.None);
}
It also has an intuitive feel, if the default itself is really what you're testing.