Nunit: Can't test object type with constraint

Created on 8 Jan 2018  ·  4Comments  ·  Source: nunit/nunit

Hello, I can't test object types with a constraint, but the classic assert is still working

enum TestEnum : ushort
{
    one = 0x01,
    two = 0x02
}

// test is okay
Assert.AreEqual(typeof(ushort), typeof(TestEnum).GetEnumUnderlyingType());

// test failed
//
// Expected: <System.Uint16>
//  But was: <System.RuntimeType>
//
Assert.That(typeof(TestEnum).GetEnumUnderlyingType(), Is.TypeOf<ushort>());
notabug

Most helpful comment

Hi @TobiasSekan. I would say that this is expected. In the classic assertion you are just comparing two types. The constraint "_tests whether the actual value is of the type supplied as an argument or a derived type._" and the type of the value typeof(TestEnum).GetEnumUnderlyingType() is System.RuntimeType.

Similar the following would also fail (with the same kind of message)

Assert.That(typeof(string), Is.TypeOf<string>());

whereas

Assert.That("", Is.TypeOf<string>());

would work. You could always rephrase the constraint as Assert.That(typeof(TestEnum).GetEnumUnderlyingType(), Is.EqualTo(typeof(ushort)));

All 4 comments

What platform and what nunit version please?

Hi @TobiasSekan. I would say that this is expected. In the classic assertion you are just comparing two types. The constraint "_tests whether the actual value is of the type supplied as an argument or a derived type._" and the type of the value typeof(TestEnum).GetEnumUnderlyingType() is System.RuntimeType.

Similar the following would also fail (with the same kind of message)

Assert.That(typeof(string), Is.TypeOf<string>());

whereas

Assert.That("", Is.TypeOf<string>());

would work. You could always rephrase the constraint as Assert.That(typeof(TestEnum).GetEnumUnderlyingType(), Is.EqualTo(typeof(ushort)));

Good catch @mikkelbu. I am going to close this as not a bug. @TobiasSekan I can see the confusion, but effectively your first parameter is TypeOf<Type> not ushort.

Okay, thank you for the information

Was this page helpful?
0 / 5 - 0 ratings