Assert.AreEqual失败,虽然它不应该 [英] Assert.AreEqual fails while it shouldn't

查看:357
本文介绍了Assert.AreEqual失败,虽然它不应该的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常奇怪的行为,我无法解释



我有以下类:

 公共类项目
{
公共虚拟INT标识{搞定;组; }

公共虚拟字符串名称{;组; }
}

和它返回一个项目<方法/ code>目标:

 公共工程GetByName方法(字符串名称)
{$ b $使用b(的Isession会话= NHibernateHelper.OpenSession())
{
计划项目= session.CreateCriteria(typeof运算(项目))
。新增(Restrictions.Eq(名,名称))
.UniqueResult<项目>();

回报率的项目;
}
}



我添加了一个单元测试来测试 GetByName方法方法:

  [TestMethod的] 
公共无效TestGetByName()
{
IProjectsRepository projectRepository =新ProjectsRepository();

VAR预期=新项目{ID = 1000,名称=PROJECT1};
VAR实际= projectRepository.GetByName(expected.Name);

Assert.AreEqual<项目>(预期,实际值);
}



但是当我运行单元测试比较这两个的类型时失败并出现以下错误的对象:




Assert.AreEqual失败。预期:其中,MyProject.NHibernate.Project取代。
实际。?< MyProject.NHibernate.Project>




为什么断言失败






是不是断言?.AreEqual 的对对象的属性只断言



根据文档:




Assert.AreEqual方法(对象,对象)



验证两个指定的对象相等。断言失败,如果
的对象是不相等的。



Assert.AreSame法



验证指定的对象变量指向同一个对象。



解决方案

您需要重写equals方法测试相等。默认情况下它会使用参考比较,因为你的预期与实际对象在内存中的不同位置会失败。下面是你应该尝试什么:

 公共类项目
{
公共虚拟INT标识{搞定;组; }

公共虚拟字符串名称{;组; }

公众覆盖布尔等于(obj对象)
{
如果(obj是项目)
{
无功即= OBJ的项目;
返回this.Id == that.Id&放大器;&安培; this.Name == that.Name;
}

返回FALSE;
}
}

您还可以检查出的guidelines的首要等于 MSDN上。


I have a really weird behavior which I cannot explain.

I have the following class:

public class Project
{
    public virtual int Id { get; set; }

    public virtual string Name { get; set; }
}

And a method which returns a Project object:

public Project GetByName(string Name)
{
    using (ISession session = NHibernateHelper.OpenSession())
    {
        Project project = session.CreateCriteria(typeof(Project))
                                 .Add(Restrictions.Eq("Name", Name))
                                 .UniqueResult<Project>();

        return project;
    }
}

I have added a Unit Test to test the GetByName method:

[TestMethod]
public void TestGetByName()
{
    IProjectsRepository projectRepository = new ProjectsRepository();

    var expected = new Project { Id = 1000, Name = "Project1" };
    var actual = projectRepository.GetByName(expected.Name);

    Assert.AreEqual<Project>(expected, actual);
}

But when I run the unit test it fails when comparing the type of the two objects with the following error:

Assert.AreEqual failed. Expected:<MyProject.NHibernate.Project>. Actual:<MyProject.NHibernate.Project>.

Why is the assertion failing?


Isn't Assert.AreEqual asserting only on the properties of the objects?

According to the documentation:

Assert.AreEqual Method (Object, Object)

Verifies that two specified objects are equal. The assertion fails if the objects are not equal.

Assert.AreSame Method

Verifies that specified object variables refer to the same object.

解决方案

You need to override the equals method to test for equality. By default it will use reference comparison, and since your expected and actual objects are in different locations in memory it will fail. Here is what you should try:

public class Project
{
    public virtual int Id { get; set; }

    public virtual string Name { get; set; }

    public override bool Equals(Object obj) 
    {
        if (obj is Project)
        {
            var that = obj as Project;
            return this.Id == that.Id && this.Name == that.Name;
        }

        return false; 
    }
}

You can also check out the guidelines for overriding equals on MSDN.

这篇关于Assert.AreEqual失败,虽然它不应该的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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