为什么Assert.IsInstanceOfType(0.GetType(),typeof(int))失败?

时间:2009-03-26 16:37:13

标签: c# unit-testing mstest

我使用Microsoft.VisualStudio.TestTools.UnitTesting;

对单元测试不熟悉

0.GetType()实际上是System.RuntimeType,那么我需要编写什么样的测试才能通过Assert.IsInstanceOfType(0.GetType(), typeof(int))

---跟进,这是我自己的用户错误... Assert.IsInstanceOfType(0, typeof(int))

2 个答案:

答案 0 :(得分:74)

将通话更改为以下

Assert.IsInstanceOfType(0, typeof(int));

第一个参数是被测对象,而不是被测对象的类型。通过传递0.GetType(),你说的是“RunTimeType”一个System.int的实例,它是假的。在封面下,这些电话只是解决了

if (typeof(int).IsInstanceOfType(0))

答案 1 :(得分:18)

看起来应该是

Assert.IsInstanceOfType(0, typeof(int))

您的表达式当前正在评估RunTimeType是否是RunTimeType的实例,而不是。

相关问题