Ejemplo n.º 1
0
        public void CanCompareEntitiesWithAssignedIds()
        {
            var obj1 = new ObjectWithAssignedId {
                Name = "Acme"
            };
            var obj2 = new ObjectWithAssignedId {
                Name = "Anvil"
            };

            obj1.Equals(null).Should().BeFalse();
            obj1 !.Equals(obj2).Should().BeFalse();

            obj1.SetAssignedIdTo("AAAAA");
            obj2.SetAssignedIdTo("AAAAA");

            // Even though the "business value signatures" are different, the persistent Ids
            // were the same.  Call me crazy, but I put that much trust into persisted Ids.
            obj1.Equals(obj2).Should().BeTrue();

            var obj3 = new ObjectWithAssignedId {
                Name = "Acme"
            };

            // Since obj1 has an Id but obj3 doesn't, they won't be equal
            // even though their signatures are the same
            obj1.Equals(obj3).Should().BeFalse();

            var obj4 = new ObjectWithAssignedId {
                Name = "Acme"
            };

            // obj3 and obj4 are both transient and share the same signature
            obj3.Equals(obj4).Should().BeTrue();
        }
        public void CanCompareEntitiesWithAssignedIds()
        {
            var object1 = new ObjectWithAssignedId {
                Name = "Acme"
            };
            var object2 = new ObjectWithAssignedId {
                Name = "Anvil"
            };

            Assert.That(object1, Is.Not.EqualTo(null));
            Assert.That(object1, Is.Not.EqualTo(object2));

            object1.SetAssignedIdTo("AAAAA");
            object2.SetAssignedIdTo("AAAAA");

            // Even though the "business value signatures" are different, the persistent Ids
            // were the same.  Call me crazy, but I put that much trust into persisted Ids.
            Assert.That(object1, Is.EqualTo(object2));

            var object3 = new ObjectWithAssignedId {
                Name = "Acme"
            };

            // Since object1 has an Id but object3 doesn't, they won't be equal
            // even though their signatures are the same
            Assert.That(object1, Is.Not.EqualTo(object3));

            var object4 = new ObjectWithAssignedId {
                Name = "Acme"
            };

            // object3 and object4 are both transient and share the same signature
            Assert.That(object3, Is.EqualTo(object4));
        }