Assert.AreEqual fails while it shouldn't

前端 未结 4 1367
小鲜肉
小鲜肉 2020-12-09 04:33

I have a really weird behavior which I cannot explain.

I have the following class:

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

           


        
相关标签:
4条回答
  • 2020-12-09 05:12

    You are comparing two different objects that hold the same data. You should overload Equals method in your Project class, and implement comparison by id there.

    0 讨论(0)
  • 2020-12-09 05:13

    An easy alternative would be this:

    Assert.IsTrue(string1 == string2, "Error");
    
    0 讨论(0)
  • 2020-12-09 05:16

    Make sure that your Project class overrides the equals method. Currently you are comparing object references, and since you have 2 different objects of Project your Assert.AreEqual() will fail.

    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;
        }
    }
    

    With the equals method in place, you can use the 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);
    }
    

    PS: If you are overriding Equals it's advised to also override the Hashcode.

        public override int GetHashCode()
        {
            var hashcode = Id.GetHashCode();
            hashCode ^= Name.GetHashCode();
            return hashCode;
        }
    

    AreSame vs AreEqual

    Assert.AreSame will still fail, even when you override the Equals method. That method actually checks if the references are pointing to the same instance. Which in your case, they will not.

    Assert.AreEqual will simply check if the objects are equal, which will be done by calling expected.Equals(actual). That will result in true once you implemented the override bool Equals() method.

    0 讨论(0)
  • 2020-12-09 05:36

    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.

    0 讨论(0)
提交回复
热议问题