In NUnit
, what is the difference between Is.TypeOf
and Is.InstanceOf
?
In the following example, I've notice that they are both return true:
public class Foo
{
public Boo GetBoo()
{
return new Boo();
}
}
public class Boo { }
And the NUnit
test method:
[Test]
public void GetBoo_WhenCalled_ReturnBoo
{
var foo = new Foo();
var result = foo.GetBoo();
Assert.That(result, Is.TypeOf<Boo>()); //return true
Assert.That(result, Is.InstanceOf<Boo>()); //return true
}
The documentation is kind of difficult to understand:
TypoOf
- tests that an object is an exact Type.
InstanceOf
- tests that an obect is an instance of a Type
It means that in contrast to TypoOf
, InstanceOf
will test also for derivites.
So, in the following example:
public class Foo
{
public Boo GetBoo()
{
return new Woo();
}
}
public class Woo : Boo { }
Test method:
[Test]
public void GetBoo_WhenCalled_ReturnBoo()
{
var foo = new Foo();
var result = foo.GetBoo();
Assert.that(result, Is.TypeOf<Boo>()); // False ("Boo")
Assert.that(result, Is.InstanceOf<Boo>()); //True ("Boo" or "Woo")
}
TypeOf
will return false cause it check if the result type is Boo
only. InstanceOf
will return true cause it check if the result type is Boo
or Woo
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.