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

发布于 2024-07-15 12:30:29 字数 307 浏览 3 评论 0原文

我对使用 Microsoft.VisualStudio.TestTools.UnitTesting 进行单元测试有点陌生;

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

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

I'm kind of new to unit testing, using 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))?

--- following up, this is my own user error... Assert.IsInstanceOfType(0, typeof(int))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

記憶穿過時間隧道 2024-07-22 12:30:29

将调用更改为以下

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

第一个参数是被测试的对象,而不是被测试对象的类型。 通过传递 0.GetType(),您所说的是“RunTimeType”是 System.int 的实例,这是错误的。 在幕后,这些电话只是决定

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

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. Under the covers thes call just resolves to

if (typeof(int).IsInstanceOfType(0))
-小熊_ 2024-07-22 12:30:29

看起来应该是

Assert.IsInstanceOfType(0, typeof(int))

您的表达式当前正在评估 RunTimeType 是否是 RunTimeType 的实例,但事实并非如此。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文