Assert.AreEqual失败,但不应该

cha*_*sos 15 c# assert mstest visual-studio-2012

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

我有以下课程:

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

    public virtual string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

以及返回Project对象的方法:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

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

[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);
}
Run Code Online (Sandbox Code Playgroud)

但是,当我运行单元测试时,在将两个对象的类型与以下错误进行比较时失败:

Assert.AreEqual失败.预期:<MyProject.NHibernate.Project>.实际:<MyProject.NHibernate.Project>.

为什么断言失败了?


是不是Assert.AreEqual仅在对象的属性上断言?

根据文件:

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

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

Assert.AreSame方法

验证指定的对象变量是否引用同一对象.

Cor*_*old 25

您需要覆盖equals方法以测试相等性.默认情况下,它将使用引用比较,并且由于您的预期对象和实际对象位于内存中的不同位置,因此它将失败.这是你应该尝试的:

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; 
    }
}
Run Code Online (Sandbox Code Playgroud)

您还可以查看MSDN上覆盖equals指南.


bas*_*bas 8

确保您的Project类重写了equals方法.目前你是比较对象引用,因为你有2个不同的对象ProjectAssert.AreEqual()会失败.

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

    public virtual string Name { get; set; }

    public override bool Equals(object o)
    {
        var result = false;
        var project = o as Project;
        if (project != null)
        {
            result = Id == project.Id;
            result &= Name.Equals(project.Name);
            return result;
        }
        return false;
    }
}
Run Code Online (Sandbox Code Playgroud)

使用equals方法,您可以使用 Assert.AreEqual

[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);
}
Run Code Online (Sandbox Code Playgroud)

PS:如果你重写Equals,建议也覆盖Hashcode.

    public override int GetHashCode()
    {
        var hashcode = Id.GetHashCode();
        hashCode ^= Name.GetHashCode();
        return hashCode;
    }
Run Code Online (Sandbox Code Playgroud)

AreSame与AreEqual

Assert.AreSame即使重写Equals方法,它仍然会失败.该方法实际上检查引用是否指向同一实例.在你的情况下,他们不会.

Assert.AreEqual将简单地检查对象是否相等,这将通过调用来完成expected.Equals(actual).一旦实现该override bool Equals()方法,这将导致true .