我是单元测试的新手,使用 Microsoft.VisualStudio.TestTools.UnitTesting
;
0.GetType()
其实就是System.RuntimeType
,那么我需要写什么样的测试才能通过Assert.IsInstanceOfType(0. GetType(), typeof(int))
?
--- 跟进,这是我自己的用户错误... Assert.IsInstanceOfType(0, typeof(int))
最佳答案
将调用更改为以下内容
Assert.IsInstanceOfType(0, typeof(int));
第一个参数是被测对象,不是被测对象的类型。通过传递 0.GetType(),您说的是“RunTimeType”是 System.int 的一个实例,它是错误的。在幕后,这些电话只是解析为
if (typeof(int).IsInstanceOfType(0))
关于c# - 为什么 Assert.IsInstanceOfType(0.GetType(), typeof(int)) 会失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/686571/