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

标签 c# unit-testing mstest

我是单元测试的新手,使用 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/

相关文章:

c# - .NET 单元测试可以使用哪种动态语言?

c# - c# wpf 列表框的加载屏幕

unit-testing - 基本单元测试,检查我是否具有对某个字段的读取访问权限 - Apex

database - MSTest 单元测试和数据库访问,无需接触实际数据库

c++ - 使用带有字符串而不是宽字符串的 Visual Studio native C++ 单元测试框架

php - 如何在 CodeIgniter 中使用 PHPUnit?

visual-studio - 为什么在 Resharper/MSTest 下调试时引用的 dll 会被锁定?

c# - 如何在 .NET 中承载无窗口 ActiveX 控件

c# - 将imageUrl转换为byte[]进行缓存

c# - TeeChart for Mono Android 无法编译