Assert.ReferenceEquals() 在 Object.ReferenceEquals() 返回“false”的情况下传递 在 Visual Studio 测试中

发布于 2024-07-17 03:59:50 字数 1570 浏览 3 评论 0原文

在尝试在 Visual Studio Professonal 2008 的测试功能中创建初始失败的单元测试时,当对象实例不时,我似乎无法让 Assert.ReferenceEquals() 正确失败/em> 等于空引用。 请注意,对于同一比较,object.ReferenceEquals() 正确返回 false

这是我的类代码:

public static class Project
{
    public static object TheObject { get; set; }

    public static void Startup(object theObject)
    {
        // ToDo: Project.Startup(): Test.
        // ToDo: Project.Startup(): Implement.
    }
}

然后这是我的测试类的关键方面:

[TestClass()]
public class ProjectTest
{
    [TestMethod()]
    public void StartupTest()
    {
        object obj = "hello";
        Project.Startup(obj);    
        Assert.ReferenceEquals(obj, Project.TheObject); // Test Passes!?!
    }
}

请注意,static void Startup(object) 方法为空,因此 静态对象 TheObject属性从未设置并保持null。 因此,显然,Assert.ReferenceEquals(obj, Project.TheObject) 应该失败,但不知何故这个测试通过了。

请注意,将

Assert.ReferenceEquals(obj, Project.TheObject)

更改为

Assert.IsTrue(object.ReferenceEquals(obj, Project.TheObject))

会导致此测试正确失败。

这看起来太简单了,但我看不出这里出了什么问题。 如果有人能指出我的方法中的错误,我将不胜感激。

预先感谢,

迈克

詹姆斯·艾弗里回答的更新:

啊,我现在感觉多么愚蠢。 我知道一定是这样的。 哇。

果然,“GoToDefinition”将我带到“Object.ReferenceEquals()”。 因此,输入“Assert.ReferenceEquals()”实际上是 System.Object.ReferenceEquals(),在我的例子中,它悄悄地返回“false”。 当然,这与实际失败的断言无关,因此测试通过。 惊人的。

谢谢詹姆斯。

In attempting to create an initial, failing unit test in Visual Studio Professonal 2008's test capabilities, I can't seem to get Assert.ReferenceEquals() to correctly fail when an object instance is not equal to a null reference. Note that object.ReferenceEquals() is correctly returning false for this same comparison.

Here is my class code:

public static class Project
{
    public static object TheObject { get; set; }

    public static void Startup(object theObject)
    {
        // ToDo: Project.Startup(): Test.
        // ToDo: Project.Startup(): Implement.
    }
}

And then here are the key aspects of my test class:

[TestClass()]
public class ProjectTest
{
    [TestMethod()]
    public void StartupTest()
    {
        object obj = "hello";
        Project.Startup(obj);    
        Assert.ReferenceEquals(obj, Project.TheObject); // Test Passes!?!
    }
}

Note that the static void Startup(object) method is empty, so the static object TheObject property is never set and remains null. So, clearly, Assert.ReferenceEquals(obj, Project.TheObject) should fail, but somehow this test passes.

Note that changing

Assert.ReferenceEquals(obj, Project.TheObject)

to

Assert.IsTrue(object.ReferenceEquals(obj, Project.TheObject))

causes this test to correctly fail.

This seems too simple, and yet I cannot see what's going wrong here. If someone can point out the error in my ways, I would be much obliged.

Thanks in advance,

Mike

Update Answered by James Avery:

Ah, an how silly I feel now. I knew it had to be something like this. Wow.

Sure enough, 'GoToDefinition' takes me to 'Object.ReferenceEquals()'. So typing "Assert.ReferenceEquals()" is really System.Object.ReferenceEquals(), which in my case was quietly returning 'false'. This, of course, has nothing to do with actually failing an assertion, so the test passes. Amazing.

Thanks James.

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

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

发布评论

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

评论(1

爺獨霸怡葒院 2024-07-24 03:59:50

您调用的 ReferenceEquals 方法是所有引用对象上可用的静态方法,它不是测试框架的一部分。 如果你看一下它返回一个布尔值,而正常的断言将是无效的。 这绝对令人困惑,.AreSame() 是您正在寻找的断言。

The ReferenceEquals method you are calling is the static method available on all reference objects, it is not part of the testing framework. If you look it is returning a boolean value whereas a normal assertion would be void. This is definitely confusing, .AreSame() is the assertion you are looking for.

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