Exemple #1
0
        public void When_including_fields_it_should_succeed_if_just_the_included_field_match()
        {
            // Arrange
            var class1 = new ClassWithSomeFieldsAndProperties
            {
                Field1    = "Lorem",
                Field2    = "ipsum",
                Field3    = "dolor",
                Property1 = "sit",
                Property2 = "amet",
                Property3 = "consectetur"
            };

            var class2 = new ClassWithSomeFieldsAndProperties {
                Field1 = "Lorem", Field2 = "ipsum"
            };

            // Act
            Action act =
                () =>
                class1.Should().BeEquivalentTo(class2, opts => opts.Including(_ => _.Field1).Including(_ => _.Field2));

            // Assert
            act.Should().NotThrow("the only selected fields have the same value");
        }
Exemple #2
0
        public void When_including_fields_it_should_fail_if_any_included_field_do_not_match()
        {
            // Arrange
            var class1 = new ClassWithSomeFieldsAndProperties
            {
                Field1    = "Lorem",
                Field2    = "ipsum",
                Field3    = "dolor",
                Property1 = "sit",
                Property2 = "amet",
                Property3 = "consectetur"
            };

            var class2 = new ClassWithSomeFieldsAndProperties {
                Field1 = "Lorem", Field2 = "ipsum"
            };

            // Act
            Action act =
                () =>
                class1.Should().BeEquivalentTo(class2,
                                               opts => opts.Including(_ => _.Field1).Including(_ => _.Field2).Including(_ => _.Field3));

            // Assert
            act.Should().Throw <XunitException>().WithMessage("Expected field class1.Field3*");
        }
Exemple #3
0
        public void When_using_IncludingAllRuntimeProperties_the_runtime_type_should_be_used_and_fields_should_be_ignored()
        {
            // Arrange
            object class1 = new ClassWithSomeFieldsAndProperties
            {
                Field1    = "Lorem",
                Field2    = "ipsum",
                Field3    = "dolor",
                Property1 = "sit",
                Property2 = "amet",
                Property3 = "consectetur"
            };

            object class2 = new ClassWithSomeFieldsAndProperties
            {
                Property1 = "sit",
                Property2 = "amet",
                Property3 = "consectetur"
            };

            // Act
            Action act =
                () => class1.Should().BeEquivalentTo(class2, opts => opts.IncludingAllRuntimeProperties());

            // Assert
            act.Should().NotThrow();
        }
Exemple #4
0
        public void Nested_fields_can_be_mapped_using_a_nested_type_and_field_names()
        {
            // Arrange
            var subject = new ClassWithSomeFieldsAndProperties {
                Field1 = "John", Field2 = "Mary"
            };

            var expectation = new ClassWithSomeFieldsAndProperties {
                Field1 = "Mary", Field2 = "John"
            };

            // Act / Assert
            subject.Should()
            .BeEquivalentTo(expectation, opt => opt
                            .WithMapping <ClassWithSomeFieldsAndProperties, ClassWithSomeFieldsAndProperties>("Field1", "Field2")
                            .WithMapping <ClassWithSomeFieldsAndProperties, ClassWithSomeFieldsAndProperties>("Field2", "Field1"));
        }
Exemple #5
0
        public void Properties_can_be_mapped_to_a_field_by_expression()
        {
            // Arrange
            var subject = new ClassWithSomeFieldsAndProperties {
                Field1 = "John",
            };

            var expectation = new ClassWithSomeFieldsAndProperties {
                Property1 = "John"
            };

            // Act / Assert
            subject.Should()
            .BeEquivalentTo(expectation, opt => opt
                            .WithMapping <ClassWithSomeFieldsAndProperties>(e => e.Property1, s => s.Field1)
                            .Including(e => e.Property1));
        }
Exemple #6
0
        public void Fields_can_be_mapped_to_a_property_by_name()
        {
            // Arrange
            var subject = new ClassWithSomeFieldsAndProperties {
                Property1 = "John"
            };

            var expectation = new ClassWithSomeFieldsAndProperties {
                Field1 = "John",
            };

            // Act / Assert
            subject.Should()
            .BeEquivalentTo(expectation, opt => opt
                            .WithMapping("Field1", "Property1")
                            .Including(e => e.Field1));
        }
Exemple #7
0
        public void Fields_can_be_mapped_by_name()
        {
            // Arrange
            var subject = new ClassWithSomeFieldsAndProperties {
                Field1 = "Hello", Field2 = "John"
            };

            var expectation = new ClassWithSomeFieldsAndProperties {
                Field1 = "John", Field2 = "Hello"
            };

            // Act / Assert
            subject.Should()
            .BeEquivalentTo(expectation, opt => opt
                            .WithMapping("Field2", "Field1")
                            .WithMapping("Field1", "Field2"));
        }
Exemple #8
0
        public void When_respecting_the_runtime_type_is_configured_the_runtime_type_should_be_used_and_both_properties_and_fields_included()
        {
            // Arrange
            object class1 = new ClassWithSomeFieldsAndProperties
            {
                Field1    = "Lorem",
                Property1 = "sit"
            };

            object class2 = new ClassWithSomeFieldsAndProperties();

            // Act
            Action act =
                () => class1.Should().BeEquivalentTo(class2, opts => opts.RespectingRuntimeTypes());

            // Assert
            act.Should().Throw <XunitException>().Which.Message.Should().Contain("Field1").And.Contain("Property1");
        }
Exemple #9
0
        public void When_both_field_and_properties_are_configured_for_inclusion_both_should_be_included()
        {
            // Arrange
            var class1 = new ClassWithSomeFieldsAndProperties
            {
                Field1    = "Lorem",
                Property1 = "sit"
            };

            var class2 = new ClassWithSomeFieldsAndProperties();

            // Act
            Action act =
                () => class1.Should().BeEquivalentTo(class2, opts => opts.IncludingFields().IncludingProperties());

            // Assert
            act.Should().Throw <XunitException>().Which.Message.Should().Contain("Field1").And.Contain("Property1");
        }
Exemple #10
0
        public void When_excluding_members_it_should_fail_if_any_non_excluded_members_are_different()
        {
            // Arrange
            var class1 = new ClassWithSomeFieldsAndProperties
            {
                Field1    = "Lorem",
                Field2    = "ipsum",
                Field3    = "dolor",
                Property1 = "sit"
            };

            var class2 = new ClassWithSomeFieldsAndProperties {
                Field1 = "Lorem", Field2 = "ipsum"
            };

            // Act
            Action act =
                () => class1.Should().BeEquivalentTo(class2, opts => opts.Excluding(_ => _.Property1));

            // Assert
            act.Should().Throw <XunitException>().WithMessage("Expected*Field3*");
        }
Exemple #11
0
        public void When_using_ExcludingMissingMembers_both_fields_and_properties_should_be_ignored()
        {
            // Arrange
            var class1 = new ClassWithSomeFieldsAndProperties
            {
                Field1    = "Lorem",
                Field2    = "ipsum",
                Field3    = "dolor",
                Property1 = "sit",
                Property2 = "amet",
                Property3 = "consectetur"
            };

            var class2 = new { Field1 = "Lorem" };

            // Act
            Action act =
                () => class1.Should().BeEquivalentTo(class2, opts => opts.ExcludingMissingMembers());

            // Assert
            act.Should().NotThrow();
        }
Exemple #12
0
        public void When_using_a_nested_equivalency_api_in_a_custom_assertion_rule_it_should_honor_the_rule()
        {
            // Arrange
            var subject = new ClassWithSomeFieldsAndProperties
            {
                Property1 = "value1",
                Property2 = "value2"
            };

            var expectation = new ClassWithSomeFieldsAndProperties
            {
                Property1 = "value1",
                Property2 = "value3"
            };

            // Act
            Action act = () => subject.Should().BeEquivalentTo(expectation, options => options
                                                               .Using <ClassWithSomeFieldsAndProperties>(ctx => ctx.Subject.Should().BeEquivalentTo(ctx.Expectation, nestedOptions => nestedOptions.Excluding(x => x.Property2)))
                                                               .WhenTypeIs <ClassWithSomeFieldsAndProperties>());

            // Assert
            act.Should().NotThrow();
        }
Exemple #13
0
        public void When_excluding_members_it_should_pass_if_only_the_excluded_members_are_different()
        {
            // Arrange
            var class1 = new ClassWithSomeFieldsAndProperties
            {
                Field1    = "Lorem",
                Field2    = "ipsum",
                Field3    = "dolor",
                Property1 = "sit"
            };

            var class2 = new ClassWithSomeFieldsAndProperties {
                Field1 = "Lorem", Field2 = "ipsum"
            };

            // Act
            Action act =
                () =>
                class1.Should().BeEquivalentTo(class2,
                                               opts => opts.Excluding(_ => _.Field3).Excluding(_ => _.Property1));

            // Assert
            act.Should().NotThrow("the non-excluded fields have the same value");
        }
Exemple #14
0
        public void When_configured_for_runtime_typing_and_properties_are_excluded_the_runtime_type_should_be_used_and_properties_should_be_ignored()
        {
            // Arrange
            object class1 = new ClassWithSomeFieldsAndProperties
            {
                Field1    = "Lorem",
                Field2    = "ipsum",
                Field3    = "dolor",
                Property1 = "sit",
                Property2 = "amet",
                Property3 = "consectetur"
            };

            object class2 = new ClassWithSomeFieldsAndProperties {
                Field1 = "Lorem", Field2 = "ipsum", Field3 = "dolor"
            };

            // Act
            Action act =
                () => class1.Should().BeEquivalentTo(class2, opts => opts.ExcludingProperties().RespectingRuntimeTypes());

            // Assert
            act.Should().NotThrow();
        }
Exemple #15
0
        public void When_excluding_properties_it_should_still_compare_fields()
        {
            // Arrange
            var class1 = new ClassWithSomeFieldsAndProperties
            {
                Field1    = "Lorem",
                Field2    = "ipsum",
                Field3    = "dolor",
                Property1 = "sit",
                Property2 = "amet",
                Property3 = "consectetur"
            };

            var class2 = new ClassWithSomeFieldsAndProperties {
                Field1 = "Lorem", Field2 = "ipsum", Field3 = "color"
            };

            // Act
            Action act =
                () => class1.Should().BeEquivalentTo(class2, opts => opts.ExcludingProperties());

            // Assert
            act.Should().Throw <XunitException>().WithMessage("*color*dolor*");
        }