I’m not sure if this is doable, but I will just give a shot.
I am calling Assert.AreEqual() method.
For the parameters, I’m passing…
- an enum value which has Int32 as the underlying type because I didn’t specify the base type
- an int (Int32) value
Assert fails because it sees that the enum is not int (which is definitely correct).
However, is there a way to make this Assert pass when the enum has the correct int value as the 2nd parameter?
I can cast the enum to int and have it a quick fix, but it’s really ugly.
I was expecting some kind of overriding a method that Assert uses to compare 2 different objects and implicitly make that enum type look like an int. However, I wasn’t successful at finding any hint/answer around that so far.
Someone suggested to create a type converter and use the TypeConverterAttribute to get around. If this works for sure and is the only way to do it, I would; however, it does seem a lot of unnecessary work.
By calling
Assert.AreEqual(enum, int), you are calling theAssert.AreEqual(object, object)method, as there is no overload that has anenumand anintas parameters.I do not think that casting the
enumto anintis ugly – in fact I think it is perfectly acceptable and a very common technique. If you were doing a normal equality comparison (ie. 1 == MyEnum.Value), you would get a compiler error as there is no implicit conversion, only an explicit one which requires a cast to work.A type converter may work, but you need to ask yourself if doing that will actually give you any real benefit for the amount of effort involved. Personally, I would just leave the cast to an
int.If you are asserting something like the return value of a method call, why not just assert based on what value you expect?
This does not require the cast as the two types are the same, and you are asserting the expected value.