简体   繁体   English

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

[英]Why does Assert.IsInstanceOfType(0.GetType(), typeof(int)) fail?

I'm kind of new to unit testing, using Microsoft.VisualStudio.TestTools.UnitTesting ; 我是一个新的单元测试,使用Microsoft.VisualStudio.TestTools.UnitTesting ;

The 0.GetType() is actually System.RuntimeType , so what kind of test do I need to write to pass Assert.IsInstanceOfType(0.GetType(), typeof(int)) ? 0.GetType()实际上是System.RuntimeType ,所以我需要编写什么样的测试来传递Assert.IsInstanceOfType(0.GetType(), typeof(int))

--- following up, this is my own user error... Assert.IsInstanceOfType(0, typeof(int)) ---跟进,这是我自己的用户错误... Assert.IsInstanceOfType(0, typeof(int))

Change the call to the following 将呼叫更改为以下内容

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

The first parameter is the object being tested, not the type of the object being tested. 第一个参数是被测试的对象,而不是被测试对象的类型。 by passing 0.GetType(), you were saying is "RunTimeType" an instance of System.int which is false. 通过传递0.GetType(),你说的是“RunTimeType”一个System.int的实例,它是假的。 Under the covers thes call just resolves to 在封面下,这些呼吁刚刚解决

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

Looks like it should be 看起来应该是这样

Assert.IsInstanceOfType(0, typeof(int))

Your expression is currently evaluating to see if RunTimeType is an instance of RunTimeType, which it isn't. 您的表达式当前正在评估RunTimeType是否是RunTimeType的实例,而不是RunTimeType。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM