Assert.AreEqual失败int和ulong的,但时间不长和uint [英] Assert.AreEqual fails for int and ulong but not long and uint

查看:115
本文介绍了Assert.AreEqual失败int和ulong的,但时间不长和uint的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗯,我希望我的处理器是不是烧了,这是因为:

Well, I hope my processor is not burned, because:

    [TestMethod]
    public void tenEqualten()
    {
        Int64 a = 10;
        UInt32 b = 10;
        Assert.AreEqual(a, b);
    }

工作得很好,但这样的:

works just fine, but this:

    [TestMethod]
    public void tenNotEqualten()
    {
        Int32 a = 10;
        UInt64 b = 10;
        Assert.AreEqual(a, b);
    }



悲惨的失败了。

fails miserably.

你有没有得到同样的结果,或者是它只是我吗?
如果是的话,任何想法,为什么?
如果这是已知问题.NET 4.5比遗憾的垃圾邮件,但我无法找到一个错误

Have you got same results, or is it just me? If yes, any ideas, why? If this is known issue for .Net 4.5 than sorry for spam, but I could not find that as a bug.

编辑:我发现重复的< A HREF =htt​​p://stackoverflow.com/questions/9420288/why-integer-zero-does-not-equal-long-zero>这里和解释的here

edit: I found a duplicate here and an explanation here

推荐答案

第一种方法您呼叫 Assert.AreEqual< T>(T预计,T实际),其中 T 是键入的Int64 ,这是因为UInt32的隐式强制转换为Int64的。这是一样的效果,如果你做了

In the first method you are calling Assert.AreEqual<T>(T expected, T actual) where T is of type Int64, this is because UInt32 is implicitly castable to Int64. It is the same effect as if you did

    [TestMethod]
    public void tenEqualten()
    {
        Int64 a = 10;
        UInt32 b = 10;
        Assert.AreEqual(a, (Int64)b);
    }

这就是为什么第一个版本通过。

That is why the first version passes.

在您所呼叫的第二个版本 Assert.AreEqual(对象预期,实际的对象),因为它们是不同的类型和失败,以不平等

In the second version you are calling Assert.AreEqual(object expected, object actual) which fails because they are different types and therefor not "equal".

您可以通过把两个数字一对象这会内让你的第一个版本的行为就像你的第二个版本让你使用断言相同超载。

You could make your first version act like your second version by putting the two numbers inside a object this would let you use the same overload of Assert.

    [TestMethod]
    public void tenEqualten()
    {
        Int64 a = 10;
        UInt32 b = 10;
        object c = a;
        object d = b;
        Assert.AreEqual(c, d);
    }

这方法将失败完全相同的方式你的 tenNotEqualten 方法将失败。

This method will fail exactly the same way your tenNotEqualten method will fail.

这篇关于Assert.AreEqual失败int和ulong的,但时间不长和uint的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆