diff --git a/Tests/FluentAssertions.Specs/Primitives/ObjectAssertionSpecs.cs b/Tests/FluentAssertions.Specs/Primitives/ObjectAssertionSpecs.cs index 6ac03c28bf..ac1acef301 100644 --- a/Tests/FluentAssertions.Specs/Primitives/ObjectAssertionSpecs.cs +++ b/Tests/FluentAssertions.Specs/Primitives/ObjectAssertionSpecs.cs @@ -14,132 +14,136 @@ namespace FluentAssertions.Specs.Primitives { public class ObjectAssertionSpecs { - #region Be / NotBe - - [Fact] - public void When_two_equal_object_are_expected_to_be_equal_it_should_not_fail() + public class Be { - // Arrange - var someObject = new ClassWithCustomEqualMethod(1); - var equalObject = new ClassWithCustomEqualMethod(1); + [Fact] + public void When_two_equal_object_are_expected_to_be_equal_it_should_not_fail() + { + // Arrange + var someObject = new ClassWithCustomEqualMethod(1); + var equalObject = new ClassWithCustomEqualMethod(1); - // Act / Assert - someObject.Should().Be(equalObject); - } + // Act / Assert + someObject.Should().Be(equalObject); + } - [Fact] - public void When_two_different_objects_are_expected_to_be_equal_it_should_fail_with_a_clear_explanation() - { - // Arrange - var someObject = new ClassWithCustomEqualMethod(1); - var nonEqualObject = new ClassWithCustomEqualMethod(2); + [Fact] + public void When_two_different_objects_are_expected_to_be_equal_it_should_fail_with_a_clear_explanation() + { + // Arrange + var someObject = new ClassWithCustomEqualMethod(1); + var nonEqualObject = new ClassWithCustomEqualMethod(2); - // Act - Action act = () => someObject.Should().Be(nonEqualObject); + // Act + Action act = () => someObject.Should().Be(nonEqualObject); - // Assert - act.Should().Throw().WithMessage( - "Expected someObject to be ClassWithCustomEqualMethod(2), but found ClassWithCustomEqualMethod(1)."); - } + // Assert + act.Should().Throw().WithMessage( + "Expected someObject to be ClassWithCustomEqualMethod(2), but found ClassWithCustomEqualMethod(1)."); + } - [Fact] - public void When_both_subject_and_expected_are_null_it_should_succeed() - { - // Arrange - object someObject = null; - object expectedObject = null; + [Fact] + public void When_both_subject_and_expected_are_null_it_should_succeed() + { + // Arrange + object someObject = null; + object expectedObject = null; - // Act / Assert - someObject.Should().Be(expectedObject); - } + // Act / Assert + someObject.Should().Be(expectedObject); + } - [Fact] - public void When_the_subject_is_null_it_should_fail() - { - // Arrange - object someObject = null; - var nonEqualObject = new ClassWithCustomEqualMethod(2); + [Fact] + public void When_the_subject_is_null_it_should_fail() + { + // Arrange + object someObject = null; + var nonEqualObject = new ClassWithCustomEqualMethod(2); - // Act - Action act = () => someObject.Should().Be(nonEqualObject); + // Act + Action act = () => someObject.Should().Be(nonEqualObject); - // Assert - act.Should().Throw() - .WithMessage("Expected someObject to be ClassWithCustomEqualMethod(2), but found ."); - } + // Assert + act.Should().Throw() + .WithMessage("Expected someObject to be ClassWithCustomEqualMethod(2), but found ."); + } - [Fact] - public void When_two_different_objects_are_expected_to_be_equal_it_should_fail_and_use_the_reason() - { - // Arrange - var someObject = new ClassWithCustomEqualMethod(1); - var nonEqualObject = new ClassWithCustomEqualMethod(2); + [Fact] + public void When_two_different_objects_are_expected_to_be_equal_it_should_fail_and_use_the_reason() + { + // Arrange + var someObject = new ClassWithCustomEqualMethod(1); + var nonEqualObject = new ClassWithCustomEqualMethod(2); - // Act - Action act = () => someObject.Should().Be(nonEqualObject, "because it should use the {0}", "reason"); + // Act + Action act = () => someObject.Should().Be(nonEqualObject, "because it should use the {0}", "reason"); - // Assert - act.Should().Throw() - .WithMessage( - "Expected someObject to be ClassWithCustomEqualMethod(2) because it should use the reason, but found ClassWithCustomEqualMethod(1)."); - } + // Assert + act.Should().Throw() + .WithMessage( + "Expected someObject to be ClassWithCustomEqualMethod(2) because it should use the reason, but found ClassWithCustomEqualMethod(1)."); + } - [Fact] - public void When_non_equal_objects_are_expected_to_be_not_equal_it_should_not_fail() - { - // Arrange - var someObject = new ClassWithCustomEqualMethod(1); - var nonEqualObject = new ClassWithCustomEqualMethod(2); + [Fact] + public void When_comparing_a_numeric_and_an_enum_for_equality_it_should_throw() + { + // Arrange + object subject = 1; + MyEnum expected = MyEnum.One; + + // Act + Action act = () => subject.Should().Be(expected); - // Act / Assert - someObject.Should().NotBe(nonEqualObject); + // Assert + act.Should().Throw(); + } } - [Fact] - public void When_two_equal_objects_are_expected_not_to_be_equal_it_should_fail_with_a_clear_explanation() + public class NotBe { - // Arrange - var someObject = new ClassWithCustomEqualMethod(1); - var equalObject = new ClassWithCustomEqualMethod(1); - - // Act - Action act = () => - someObject.Should().NotBe(equalObject); + [Fact] + public void When_non_equal_objects_are_expected_to_be_not_equal_it_should_not_fail() + { + // Arrange + var someObject = new ClassWithCustomEqualMethod(1); + var nonEqualObject = new ClassWithCustomEqualMethod(2); - // Assert - act.Should().Throw().WithMessage( - "Did not expect someObject to be equal to ClassWithCustomEqualMethod(1)."); - } + // Act / Assert + someObject.Should().NotBe(nonEqualObject); + } - [Fact] - public void When_two_equal_objects_are_expected_not_to_be_equal_it_should_fail_and_use_the_reason() - { - // Arrange - var someObject = new ClassWithCustomEqualMethod(1); - var equalObject = new ClassWithCustomEqualMethod(1); - - // Act - Action act = () => - someObject.Should().NotBe(equalObject, "because we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw().WithMessage( - "Did not expect someObject to be equal to ClassWithCustomEqualMethod(1) " + - "because we want to test the failure message."); - } + [Fact] + public void When_two_equal_objects_are_expected_not_to_be_equal_it_should_fail_with_a_clear_explanation() + { + // Arrange + var someObject = new ClassWithCustomEqualMethod(1); + var equalObject = new ClassWithCustomEqualMethod(1); - [Fact] - public void When_comparing_a_numeric_and_an_enum_for_equality_it_should_throw() - { - // Arrange - object subject = 1; - MyEnum expected = MyEnum.One; + // Act + Action act = () => + someObject.Should().NotBe(equalObject); - // Act - Action act = () => subject.Should().Be(expected); + // Assert + act.Should().Throw().WithMessage( + "Did not expect someObject to be equal to ClassWithCustomEqualMethod(1)."); + } - // Assert - act.Should().Throw(); + [Fact] + public void When_two_equal_objects_are_expected_not_to_be_equal_it_should_fail_and_use_the_reason() + { + // Arrange + var someObject = new ClassWithCustomEqualMethod(1); + var equalObject = new ClassWithCustomEqualMethod(1); + + // Act + Action act = () => + someObject.Should().NotBe(equalObject, "because we want to test the failure {0}", "message"); + + // Assert + act.Should().Throw().WithMessage( + "Did not expect someObject to be equal to ClassWithCustomEqualMethod(1) " + + "because we want to test the failure message."); + } } private enum MyEnum @@ -148,810 +152,810 @@ private enum MyEnum Two = 2 } - #endregion - - #region BeNull / BeNotNull - - [Fact] - public void Should_succeed_when_asserting_null_object_to_be_null() - { - // Arrange - object someObject = null; - - // Act / Assert - someObject.Should().BeNull(); - } - - [Fact] - public void Should_fail_when_asserting_non_null_object_to_be_null() - { - // Arrange - var someObject = new object(); - - // Act - Action act = () => someObject.Should().BeNull(); - - // Assert - act.Should().Throw(); - } - - [Fact] - public void When_a_non_null_object_is_expected_to_be_null_it_should_fail() - { - // Arrange - var someObject = new object(); - - // Act - Action act = () => someObject.Should().BeNull("because we want to test the failure {0}", "message"); - - // Assert - act - .Should().Throw() - .Where(e => e.Message.StartsWith( - "Expected someObject to be because we want to test the failure message, but found System.Object", - StringComparison.Ordinal)); - } - - [Fact] - public void Should_succeed_when_asserting_non_null_object_not_to_be_null() - { - // Arrange - var someObject = new object(); - - // Act / Assert - someObject.Should().NotBeNull(); - } - - [Fact] - public void Should_fail_when_asserting_null_object_not_to_be_null() - { - // Arrange - object someObject = null; - - // Act - Action act = () => someObject.Should().NotBeNull(); - - // Assert - act.Should().Throw(); - } - - [Fact] - public void Should_fail_with_descriptive_message_when_asserting_null_object_not_to_be_null() - { - // Arrange - object someObject = null; - - // Act - Action act = () => someObject.Should().NotBeNull("because we want to test the failure {0}", "message"); - - // Assert - act.Should().Throw().WithMessage( - "Expected someObject not to be because we want to test the failure message."); - } - - #endregion - - #region BeOfType / NotBeOfType - - [Fact] - public void When_object_type_is_matched_against_null_type_exactly_it_should_throw() + public class BeNull { - // Arrange - var someObject = new object(); + [Fact] + public void Should_succeed_when_asserting_null_object_to_be_null() + { + // Arrange + object someObject = null; - // Act - Action act = () => someObject.Should().BeOfType(null); + // Act / Assert + someObject.Should().BeNull(); + } - // Assert - act.Should().Throw() - .WithParameterName("expectedType"); - } + [Fact] + public void Should_fail_when_asserting_non_null_object_to_be_null() + { + // Arrange + var someObject = new object(); - [Fact] - public void When_object_type_is_matched_negatively_against_null_type_exactly_it_should_throw() - { - // Arrange - var someObject = new object(); + // Act + Action act = () => someObject.Should().BeNull(); - // Act - Action act = () => someObject.Should().NotBeOfType(null); + // Assert + act.Should().Throw(); + } - // Assert - act.Should().Throw() - .WithParameterName("unexpectedType"); + [Fact] + public void When_a_non_null_object_is_expected_to_be_null_it_should_fail() + { + // Arrange + var someObject = new object(); + + // Act + Action act = () => someObject.Should().BeNull("because we want to test the failure {0}", "message"); + + // Assert + act + .Should().Throw() + .Where(e => e.Message.StartsWith( + "Expected someObject to be because we want to test the failure message, but found System.Object", + StringComparison.Ordinal)); + } } - [Fact] - public void When_object_type_is_exactly_equal_to_the_specified_type_it_should_not_fail() + public class BeNotNull { - // Arrange - var someObject = new Exception(); - - // Act - Action act = () => someObject.Should().BeOfType(); + [Fact] + public void Should_succeed_when_asserting_non_null_object_not_to_be_null() + { + // Arrange + var someObject = new object(); - // Assert - act.Should().NotThrow(); - } + // Act / Assert + someObject.Should().NotBeNull(); + } - [Fact] - public void When_object_type_is_value_type_and_matches_received_type_should_not_fail_and_assert_correctly() - { - // Arrange - int valueTypeObject = 42; + [Fact] + public void Should_fail_when_asserting_null_object_not_to_be_null() + { + // Arrange + object someObject = null; - // Act - Action act = () => valueTypeObject.Should().BeOfType(typeof(int)); + // Act + Action act = () => someObject.Should().NotBeNull(); - // Assert - act.Should().NotThrow(); - } + // Assert + act.Should().Throw(); + } - [Fact] - public void When_object_is_matched_against_a_null_type_it_should_throw() - { - // Arrange - int valueTypeObject = 42; + [Fact] + public void Should_fail_with_descriptive_message_when_asserting_null_object_not_to_be_null() + { + // Arrange + object someObject = null; - // Act - Action act = () => valueTypeObject.Should().BeOfType(null); + // Act + Action act = () => someObject.Should().NotBeNull("because we want to test the failure {0}", "message"); - // Assert - act.Should().Throw() - .WithParameterName("expectedType"); + // Assert + act.Should().Throw().WithMessage( + "Expected someObject not to be because we want to test the failure message."); + } } - [Fact] - public void When_null_object_is_matched_against_a_type_it_should_throw() + public class BeOfType { - // Arrange - int? valueTypeObject = null; + [Fact] + public void When_object_type_is_matched_against_null_type_exactly_it_should_throw() + { + // Arrange + var someObject = new object(); - // Act - Action act = () => valueTypeObject.Should().BeOfType(typeof(int), "because we want to test the failure {0}", "message"); + // Act + Action act = () => someObject.Should().BeOfType(null); - // Assert - act.Should().Throw() - .WithMessage("*type to be System.Int32*because we want to test the failure message*"); - } + // Assert + act.Should().Throw() + .WithParameterName("expectedType"); + } - [Fact] - public void When_object_is_matched_negatively_against_a_null_type_it_should_throw() - { - // Arrange - int valueTypeObject = 42; + [Fact] + public void When_object_type_is_exactly_equal_to_the_specified_type_it_should_not_fail() + { + // Arrange + var someObject = new Exception(); - // Act - Action act = () => valueTypeObject.Should().NotBeOfType(null); + // Act + Action act = () => someObject.Should().BeOfType(); - // Assert - act.Should().Throw() - .WithParameterName("unexpectedType"); - } + // Assert + act.Should().NotThrow(); + } - [Fact] - public void When_object_type_is_value_type_and_doesnt_match_received_type_as_expected_should_not_fail_and_assert_correctly() - { - // Arrange - int valueTypeObject = 42; + [Fact] + public void When_object_type_is_value_type_and_matches_received_type_should_not_fail_and_assert_correctly() + { + // Arrange + int valueTypeObject = 42; - // Act - Action act = () => valueTypeObject.Should().NotBeOfType(typeof(double)); + // Act + Action act = () => valueTypeObject.Should().BeOfType(typeof(int)); - // Assert - act.Should().NotThrow(); - } + // Assert + act.Should().NotThrow(); + } - [Fact] - public void When_null_object_is_matched_negatively_against_a_type_it_should_throw() - { - // Arrange - int? valueTypeObject = null; + [Fact] + public void When_object_is_matched_against_a_null_type_it_should_throw() + { + // Arrange + int valueTypeObject = 42; - // Act - Action act = () => valueTypeObject.Should().NotBeOfType(typeof(int), "because we want to test the failure {0}", "message"); + // Act + Action act = () => valueTypeObject.Should().BeOfType(null); - // Assert - act.Should().Throw() - .WithMessage("*type not to be System.Int32*because we want to test the failure message*"); - } + // Assert + act.Should().Throw() + .WithParameterName("expectedType"); + } - [Fact] - public void When_object_type_is_value_type_and_matches_received_type_not_as_expected_should_fail() - { - // Arrange - int valueTypeObject = 42; - var expectedType = typeof(int); + [Fact] + public void When_null_object_is_matched_against_a_type_it_should_throw() + { + // Arrange + int? valueTypeObject = null; - // Act - Action act = () => valueTypeObject.Should().NotBeOfType(expectedType); + // Act + Action act = () => valueTypeObject.Should().BeOfType(typeof(int), "because we want to test the failure {0}", "message"); - // Assert - act.Should().Throw().WithMessage($"Expected type not to be [{expectedType.AssemblyQualifiedName}], but it is."); - } + // Assert + act.Should().Throw() + .WithMessage("*type to be System.Int32*because we want to test the failure message*"); + } - [Fact] - public void When_object_type_is_value_type_and_doesnt_match_received_type_should_fail() - { - // Arrange - int valueTypeObject = 42; - var doubleType = typeof(double); + [Fact] + public void When_object_type_is_value_type_and_doesnt_match_received_type_should_fail() + { + // Arrange + int valueTypeObject = 42; + var doubleType = typeof(double); - // Act - Action act = () => valueTypeObject.Should().BeOfType(doubleType); + // Act + Action act = () => valueTypeObject.Should().BeOfType(doubleType); - // Assert - act.Should().Throw().WithMessage($"Expected type to be {doubleType}, but found {valueTypeObject.GetType()}."); - } + // Assert + act.Should().Throw().WithMessage($"Expected type to be {doubleType}, but found {valueTypeObject.GetType()}."); + } - [Fact] - public void When_object_is_of_the_expected_type_it_should_cast_the_returned_object_for_chaining() - { - // Arrange - var someObject = new Exception("Actual Message"); + [Fact] + public void When_object_is_of_the_expected_type_it_should_cast_the_returned_object_for_chaining() + { + // Arrange + var someObject = new Exception("Actual Message"); - // Act - Action act = () => someObject.Should().BeOfType().Which.Message.Should().Be("Other Message"); + // Act + Action act = () => someObject.Should().BeOfType().Which.Message.Should().Be("Other Message"); - // Assert - act.Should().Throw().WithMessage("*Expected*Other*Actual*"); - } + // Assert + act.Should().Throw().WithMessage("*Expected*Other*Actual*"); + } - [Fact] - public void When_object_type_is_different_than_expected_type_it_should_fail_with_descriptive_message() - { - // Arrange - var someObject = new object(); + [Fact] + public void When_object_type_is_different_than_expected_type_it_should_fail_with_descriptive_message() + { + // Arrange + var someObject = new object(); - // Act - Action act = () => someObject.Should().BeOfType("because they are {0} {1}", "of different", "type"); + // Act + Action act = () => someObject.Should().BeOfType("because they are {0} {1}", "of different", "type"); - // Assert - act.Should().Throw().WithMessage( - "Expected type to be System.Int32 because they are of different type, but found System.Object."); - } + // Assert + act.Should().Throw().WithMessage( + "Expected type to be System.Int32 because they are of different type, but found System.Object."); + } - [Fact] - public void When_asserting_the_type_of_a_null_object_it_should_throw() - { - // Arrange - object someObject = null; + [Fact] + public void When_asserting_the_type_of_a_null_object_it_should_throw() + { + // Arrange + object someObject = null; - // Act - Action act = () => someObject.Should().BeOfType(); + // Act + Action act = () => someObject.Should().BeOfType(); - // Assert - act.Should().Throw() - .WithMessage("Expected someObject to be System.Int32, but found ."); - } + // Assert + act.Should().Throw() + .WithMessage("Expected someObject to be System.Int32, but found ."); + } - [Fact] - public void When_object_type_is_same_as_expected_type_but_in_different_assembly_it_should_fail_with_assembly_qualified_name() - { - // Arrange - var typeFromOtherAssembly = - new AssemblyA.ClassA().ReturnClassC(); + [Fact] + public void When_object_type_is_same_as_expected_type_but_in_different_assembly_it_should_fail_with_assembly_qualified_name() + { + // Arrange + var typeFromOtherAssembly = + new AssemblyA.ClassA().ReturnClassC(); - // Act + // Act #pragma warning disable 436 // disable the warning on conflicting types, as this is the intention for the spec - Action act = () => - typeFromOtherAssembly.Should().BeOfType(); + Action act = () => + typeFromOtherAssembly.Should().BeOfType(); #pragma warning restore 436 - // Assert - act.Should().Throw() - .WithMessage("Expected type to be [AssemblyB.ClassC, FluentAssertions.Specs*], but found [AssemblyB.ClassC, AssemblyB*]."); - } - - [Fact] - public void When_object_type_is_a_subclass_of_the_expected_type_it_should_fail() - { - // Arrange - var someObject = new DummyImplementingClass(); - - // Act - Action act = () => someObject.Should().BeOfType(); - - // Assert - act.Should().Throw().WithMessage( - "Expected type to be FluentAssertions*DummyBaseClass, but found FluentAssertions*DummyImplementingClass."); - } - - #endregion - - #region BeAssignableTo + // Assert + act.Should().Throw() + .WithMessage("Expected type to be [AssemblyB.ClassC, FluentAssertions.Specs*], but found [AssemblyB.ClassC, AssemblyB*]."); + } - [Fact] - public void When_object_type_is_matched_against_null_type_it_should_throw() - { - // Arrange - var someObject = new object(); + [Fact] + public void When_object_type_is_a_subclass_of_the_expected_type_it_should_fail() + { + // Arrange + var someObject = new DummyImplementingClass(); - // Act - Action act = () => someObject.Should().BeAssignableTo(null); + // Act + Action act = () => someObject.Should().BeOfType(); - // Assert - act.Should().Throw() - .WithParameterName("type"); + // Assert + act.Should().Throw().WithMessage( + "Expected type to be FluentAssertions*DummyBaseClass, but found FluentAssertions*DummyImplementingClass."); + } } - [Fact] - public void When_its_own_type_it_should_succeed() + public class NotBeOfType { - // Arrange - var someObject = new DummyImplementingClass(); + [Fact] + public void When_object_type_is_matched_negatively_against_null_type_exactly_it_should_throw() + { + // Arrange + var someObject = new object(); - // Act / Assert - someObject.Should().BeAssignableTo(); - } + // Act + Action act = () => someObject.Should().NotBeOfType(null); - [Fact] - public void When_its_base_type_it_should_succeed() - { - // Arrange - var someObject = new DummyImplementingClass(); + // Assert + act.Should().Throw() + .WithParameterName("unexpectedType"); + } - // Act / Assert - someObject.Should().BeAssignableTo(); - } + [Fact] + public void When_object_is_matched_negatively_against_a_null_type_it_should_throw() + { + // Arrange + int valueTypeObject = 42; - [Fact] - public void When_an_implemented_interface_type_it_should_succeed() - { - // Arrange - var someObject = new DummyImplementingClass(); + // Act + Action act = () => valueTypeObject.Should().NotBeOfType(null); - // Act / Assert - someObject.Should().BeAssignableTo(); - } + // Assert + act.Should().Throw() + .WithParameterName("unexpectedType"); + } - [Fact] - public void When_an_unrelated_type_it_should_fail_with_a_descriptive_message() - { - // Arrange - var someObject = new DummyImplementingClass(); - Action act = () => someObject.Should().BeAssignableTo("because we want to test the failure {0}", "message"); + [Fact] + public void When_object_type_is_value_type_and_doesnt_match_received_type_as_expected_should_not_fail_and_assert_correctly() + { + // Arrange + int valueTypeObject = 42; - // Act / Assert - act.Should().Throw() - .WithMessage($"*assignable to {typeof(DateTime)}*failure message*{typeof(DummyImplementingClass)} is not*"); - } + // Act + Action act = () => valueTypeObject.Should().NotBeOfType(typeof(double)); - [Fact] - public void When_to_the_expected_type_it_should_cast_the_returned_object_for_chaining() - { - // Arrange - var someObject = new Exception("Actual Message"); + // Assert + act.Should().NotThrow(); + } - // Act - Action act = () => someObject.Should().BeAssignableTo().Which.Message.Should().Be("Other Message"); + [Fact] + public void When_null_object_is_matched_negatively_against_a_type_it_should_throw() + { + // Arrange + int? valueTypeObject = null; - // Assert - act.Should().Throw().WithMessage("*Expected*Other*Actual*"); - } + // Act + Action act = () => valueTypeObject.Should().NotBeOfType(typeof(int), "because we want to test the failure {0}", "message"); - [Fact] - public void When_a_null_instance_is_asserted_to_be_assignableOfT_it_should_fail() - { - // Arrange - object someObject = null; + // Assert + act.Should().Throw() + .WithMessage("*type not to be System.Int32*because we want to test the failure message*"); + } - // Act - Action act = () => + [Fact] + public void When_object_type_is_value_type_and_matches_received_type_not_as_expected_should_fail() { - using var _ = new AssertionScope(); - someObject.Should().BeAssignableTo("because we want to test the failure {0}", "message"); - }; + // Arrange + int valueTypeObject = 42; + var expectedType = typeof(int); - // Assert - act.Should().Throw() - .WithMessage($"*assignable to {typeof(DateTime)}*failure message*found *"); - } + // Act + Action act = () => valueTypeObject.Should().NotBeOfType(expectedType); - [Fact] - public void When_its_own_type_instance_it_should_succeed() - { - // Arrange - var someObject = new DummyImplementingClass(); - - // Act / Assert - someObject.Should().BeAssignableTo(typeof(DummyImplementingClass)); + // Assert + act.Should().Throw().WithMessage($"Expected type not to be [{expectedType.AssemblyQualifiedName}], but it is."); + } } - [Fact] - public void When_its_base_type_instance_it_should_succeed() + public class BeAssignableTo { - // Arrange - var someObject = new DummyImplementingClass(); + [Fact] + public void When_object_type_is_matched_against_null_type_it_should_throw() + { + // Arrange + var someObject = new object(); - // Act / Assert - someObject.Should().BeAssignableTo(typeof(DummyBaseClass)); - } + // Act + Action act = () => someObject.Should().BeAssignableTo(null); - [Fact] - public void When_an_implemented_interface_type_instance_it_should_succeed() - { - // Arrange - var someObject = new DummyImplementingClass(); + // Assert + act.Should().Throw() + .WithParameterName("type"); + } - // Act / Assert - someObject.Should().BeAssignableTo(typeof(IDisposable)); - } + [Fact] + public void When_its_own_type_it_should_succeed() + { + // Arrange + var someObject = new DummyImplementingClass(); - [Fact] - public void When_an_implemented_open_generic_interface_type_instance_it_should_succeed() - { - // Arrange - var someObject = new System.Collections.Generic.List(); + // Act / Assert + someObject.Should().BeAssignableTo(); + } - // Act / Assert - someObject.Should().BeAssignableTo(typeof(System.Collections.Generic.IList<>)); - } + [Fact] + public void When_its_base_type_it_should_succeed() + { + // Arrange + var someObject = new DummyImplementingClass(); - [Fact] - public void When_a_null_instance_is_asserted_to_be_assignable_it_should_fail_with_a_descriptive_message() - { - // Arrange - object someObject = null; + // Act / Assert + someObject.Should().BeAssignableTo(); + } - // Act - Action act = () => + [Fact] + public void When_an_implemented_interface_type_it_should_succeed() { - using var _ = new AssertionScope(); - someObject.Should().BeAssignableTo(typeof(DateTime), "because we want to test the failure {0}", "message"); - }; - - // Assert - act.Should().Throw() - .WithMessage($"*assignable to {typeof(DateTime)}*failure message*found *"); - } + // Arrange + var someObject = new DummyImplementingClass(); - [Fact] - public void When_an_unrelated_type_instance_it_should_fail_with_a_descriptive_message() - { - // Arrange - var someObject = new DummyImplementingClass(); - Action act = () => someObject.Should().BeAssignableTo(typeof(DateTime), "because we want to test the failure {0}", "message"); + // Act / Assert + someObject.Should().BeAssignableTo(); + } - // Act / Assert - act.Should().Throw() - .WithMessage($"*assignable to {typeof(DateTime)}*failure message*{typeof(DummyImplementingClass)} is not*"); - } + [Fact] + public void When_an_unrelated_type_it_should_fail_with_a_descriptive_message() + { + // Arrange + var someObject = new DummyImplementingClass(); + Action act = () => someObject.Should().BeAssignableTo("because we want to test the failure {0}", "message"); - [Fact] - public void When_unrelated_to_open_generic_type_it_should_fail_with_a_descriptive_message() - { - // Arrange - var someObject = new DummyImplementingClass(); - Action act = () => someObject.Should().BeAssignableTo(typeof(System.Collections.Generic.IList<>), "because we want to test the failure {0}", "message"); + // Act / Assert + act.Should().Throw() + .WithMessage($"*assignable to {typeof(DateTime)}*failure message*{typeof(DummyImplementingClass)} is not*"); + } - // Act / Assert - act.Should().Throw() - .WithMessage($"*assignable to {typeof(System.Collections.Generic.IList<>)}*failure message*{typeof(DummyImplementingClass)} is not*"); - } + [Fact] + public void When_to_the_expected_type_it_should_cast_the_returned_object_for_chaining() + { + // Arrange + var someObject = new Exception("Actual Message"); - [Fact] - public void When_an_assertion_fails_on_BeAssignableTo_succeeding_message_should_be_included() - { - // Act - Action act = () => - { - using var _ = new AssertionScope(); - var item = string.Empty; - item.Should().BeAssignableTo(); - item.Should().BeAssignableTo(); - }; - - // Assert - act.Should().Throw() - .WithMessage( - "Expected * to be assignable to System.Int32, but System.String is not.*" + - "Expected * to be assignable to System.Int64, but System.String is not."); - } + // Act + Action act = () => someObject.Should().BeAssignableTo().Which.Message.Should().Be("Other Message"); - #endregion + // Assert + act.Should().Throw().WithMessage("*Expected*Other*Actual*"); + } - #region NotBeAssignableTo + [Fact] + public void When_a_null_instance_is_asserted_to_be_assignableOfT_it_should_fail() + { + // Arrange + object someObject = null; + + // Act + Action act = () => + { + using var _ = new AssertionScope(); + someObject.Should().BeAssignableTo("because we want to test the failure {0}", "message"); + }; + + // Assert + act.Should().Throw() + .WithMessage($"*assignable to {typeof(DateTime)}*failure message*found *"); + } - [Fact] - public void When_object_type_is_matched_negatively_against_null_type_it_should_throw() - { - // Arrange - var someObject = new object(); + [Fact] + public void When_its_own_type_instance_it_should_succeed() + { + // Arrange + var someObject = new DummyImplementingClass(); - // Act - Action act = () => someObject.Should().NotBeAssignableTo(null); + // Act / Assert + someObject.Should().BeAssignableTo(typeof(DummyImplementingClass)); + } - // Assert - act.Should().Throw() - .WithParameterName("type"); - } + [Fact] + public void When_its_base_type_instance_it_should_succeed() + { + // Arrange + var someObject = new DummyImplementingClass(); - [Fact] - public void When_its_own_type_and_asserting_not_assignable_it_should_fail_with_a_useful_message() - { - // Arrange - var someObject = new DummyImplementingClass(); - Action act = () => someObject.Should().NotBeAssignableTo("because we want to test the failure {0}", "message"); + // Act / Assert + someObject.Should().BeAssignableTo(typeof(DummyBaseClass)); + } - // Act / Assert - act.Should().Throw() - .WithMessage($"*not be assignable to {typeof(DummyImplementingClass)}*failure message*{typeof(DummyImplementingClass)} is*"); - } + [Fact] + public void When_an_implemented_interface_type_instance_it_should_succeed() + { + // Arrange + var someObject = new DummyImplementingClass(); - [Fact] - public void When_its_base_type_and_asserting_not_assignable_it_should_fail_with_a_useful_message() - { - // Arrange - var someObject = new DummyImplementingClass(); - Action act = () => someObject.Should().NotBeAssignableTo("because we want to test the failure {0}", "message"); + // Act / Assert + someObject.Should().BeAssignableTo(typeof(IDisposable)); + } - // Act / Assert - act.Should().Throw() - .WithMessage($"*not be assignable to {typeof(DummyBaseClass)}*failure message*{typeof(DummyImplementingClass)} is*"); - } + [Fact] + public void When_an_implemented_open_generic_interface_type_instance_it_should_succeed() + { + // Arrange + var someObject = new System.Collections.Generic.List(); - [Fact] - public void When_an_implemented_interface_type_and_asserting_not_assignable_it_should_fail_with_a_useful_message() - { - // Arrange - var someObject = new DummyImplementingClass(); - Action act = () => someObject.Should().NotBeAssignableTo("because we want to test the failure {0}", "message"); + // Act / Assert + someObject.Should().BeAssignableTo(typeof(System.Collections.Generic.IList<>)); + } - // Act / Assert - act.Should().Throw() - .WithMessage($"*not be assignable to {typeof(IDisposable)}*failure message*{typeof(DummyImplementingClass)} is*"); - } + [Fact] + public void When_a_null_instance_is_asserted_to_be_assignable_it_should_fail_with_a_descriptive_message() + { + // Arrange + object someObject = null; + + // Act + Action act = () => + { + using var _ = new AssertionScope(); + someObject.Should().BeAssignableTo(typeof(DateTime), "because we want to test the failure {0}", "message"); + }; + + // Assert + act.Should().Throw() + .WithMessage($"*assignable to {typeof(DateTime)}*failure message*found *"); + } - [Fact] - public void When_an_unrelated_type_and_asserting_not_assignable_it_should_succeed() - { - // Arrange - var someObject = new DummyImplementingClass(); + [Fact] + public void When_an_unrelated_type_instance_it_should_fail_with_a_descriptive_message() + { + // Arrange + var someObject = new DummyImplementingClass(); + Action act = () => someObject.Should().BeAssignableTo(typeof(DateTime), "because we want to test the failure {0}", "message"); - // Act / Assert - someObject.Should().NotBeAssignableTo(); - } + // Act / Assert + act.Should().Throw() + .WithMessage($"*assignable to {typeof(DateTime)}*failure message*{typeof(DummyImplementingClass)} is not*"); + } - [Fact] - public void When_not_to_the_unexpected_type_and_asserting_not_assignable_it_should_not_cast_the_returned_object_for_chaining() - { - // Arrange - var someObject = new Exception("Actual Message"); + [Fact] + public void When_unrelated_to_open_generic_type_it_should_fail_with_a_descriptive_message() + { + // Arrange + var someObject = new DummyImplementingClass(); + Action act = () => someObject.Should().BeAssignableTo(typeof(System.Collections.Generic.IList<>), "because we want to test the failure {0}", "message"); - // Act - Action act = () => someObject.Should().NotBeAssignableTo() - .And.Subject.Should().BeOfType() - .Which.Message.Should().Be("Other Message"); + // Act / Assert + act.Should().Throw() + .WithMessage($"*assignable to {typeof(System.Collections.Generic.IList<>)}*failure message*{typeof(DummyImplementingClass)} is not*"); + } - // Assert - act.Should().Throw().WithMessage("*Expected*Other*Actual*"); + [Fact] + public void When_an_assertion_fails_on_BeAssignableTo_succeeding_message_should_be_included() + { + // Act + Action act = () => + { + using var _ = new AssertionScope(); + var item = string.Empty; + item.Should().BeAssignableTo(); + item.Should().BeAssignableTo(); + }; + + // Assert + act.Should().Throw() + .WithMessage( + "Expected * to be assignable to System.Int32, but System.String is not.*" + + "Expected * to be assignable to System.Int64, but System.String is not."); + } } - [Fact] - public void When_its_own_type_instance_and_asserting_not_assignable_it_should_fail_with_a_useful_message() + public class NotBeAssignableTo { - // Arrange - var someObject = new DummyImplementingClass(); - Action act = () => someObject.Should().NotBeAssignableTo(typeof(DummyImplementingClass), "because we want to test the failure {0}", "message"); + [Fact] + public void When_object_type_is_matched_negatively_against_null_type_it_should_throw() + { + // Arrange + var someObject = new object(); - // Act / Assert - act.Should().Throw() - .WithMessage($"*not be assignable to {typeof(DummyImplementingClass)}*failure message*{typeof(DummyImplementingClass)} is*"); - } + // Act + Action act = () => someObject.Should().NotBeAssignableTo(null); - [Fact] - public void When_its_base_type_instance_and_asserting_not_assignable_it_should_fail_with_a_useful_message() - { - // Arrange - var someObject = new DummyImplementingClass(); - Action act = () => someObject.Should().NotBeAssignableTo(typeof(DummyBaseClass), "because we want to test the failure {0}", "message"); + // Assert + act.Should().Throw() + .WithParameterName("type"); + } - // Act / Assert - act.Should().Throw() - .WithMessage($"*not be assignable to {typeof(DummyBaseClass)}*failure message*{typeof(DummyImplementingClass)} is*"); - } + [Fact] + public void When_its_own_type_and_asserting_not_assignable_it_should_fail_with_a_useful_message() + { + // Arrange + var someObject = new DummyImplementingClass(); + Action act = () => someObject.Should().NotBeAssignableTo("because we want to test the failure {0}", "message"); - [Fact] - public void When_an_implemented_interface_type_instance_and_asserting_not_assignable_it_should_fail_with_a_useful_message() - { - // Arrange - var someObject = new DummyImplementingClass(); - Action act = () => someObject.Should().NotBeAssignableTo(typeof(IDisposable), "because we want to test the failure {0}", "message"); + // Act / Assert + act.Should().Throw() + .WithMessage($"*not be assignable to {typeof(DummyImplementingClass)}*failure message*{typeof(DummyImplementingClass)} is*"); + } - // Act / Assert - act.Should().Throw() - .WithMessage($"*not be assignable to {typeof(IDisposable)}*failure message*{typeof(DummyImplementingClass)} is*"); - } + [Fact] + public void When_its_base_type_and_asserting_not_assignable_it_should_fail_with_a_useful_message() + { + // Arrange + var someObject = new DummyImplementingClass(); + Action act = () => someObject.Should().NotBeAssignableTo("because we want to test the failure {0}", "message"); - [Fact] - public void When_an_implemented_open_generic_interface_type_instance_and_asserting_not_assignable_it_should_fail_with_a_useful_message() - { - // Arrange - var someObject = new System.Collections.Generic.List(); - Action act = () => someObject.Should().NotBeAssignableTo(typeof(System.Collections.Generic.IList<>), "because we want to test the failure {0}", "message"); + // Act / Assert + act.Should().Throw() + .WithMessage($"*not be assignable to {typeof(DummyBaseClass)}*failure message*{typeof(DummyImplementingClass)} is*"); + } - // Act / Assert - act.Should().Throw() - .WithMessage($"*not be assignable to {typeof(System.Collections.Generic.IList<>)}*failure message*{typeof(System.Collections.Generic.List)} is*"); - } + [Fact] + public void When_an_implemented_interface_type_and_asserting_not_assignable_it_should_fail_with_a_useful_message() + { + // Arrange + var someObject = new DummyImplementingClass(); + Action act = () => someObject.Should().NotBeAssignableTo("because we want to test the failure {0}", "message"); - [Fact] - public void When_a_null_instance_is_asserted_to_not_be_assignable_it_should_fail_with_a_descriptive_message() - { - // Arrange - object someObject = null; + // Act / Assert + act.Should().Throw() + .WithMessage($"*not be assignable to {typeof(IDisposable)}*failure message*{typeof(DummyImplementingClass)} is*"); + } - // Act - Action act = () => + [Fact] + public void When_an_unrelated_type_and_asserting_not_assignable_it_should_succeed() { - using var _ = new AssertionScope(); - someObject.Should().NotBeAssignableTo(typeof(DateTime), "because we want to test the failure {0}", "message"); - }; + // Arrange + var someObject = new DummyImplementingClass(); - // Assert - act.Should().Throw() - .WithMessage($"*not be assignable to {typeof(DateTime)}*failure message*found *"); - } + // Act / Assert + someObject.Should().NotBeAssignableTo(); + } - [Fact] - public void When_an_unrelated_type_instance_and_asserting_not_assignable_it_should_succeed() - { - // Arrange - var someObject = new DummyImplementingClass(); + [Fact] + public void When_not_to_the_unexpected_type_and_asserting_not_assignable_it_should_not_cast_the_returned_object_for_chaining() + { + // Arrange + var someObject = new Exception("Actual Message"); - // Act / Assert - someObject.Should().NotBeAssignableTo(typeof(DateTime), "because we want to test the failure {0}", "message"); - } + // Act + Action act = () => someObject.Should().NotBeAssignableTo() + .And.Subject.Should().BeOfType() + .Which.Message.Should().Be("Other Message"); - [Fact] - public void When_unrelated_to_open_generic_type_and_asserting_not_assignable_it_should_succeed() - { - // Arrange - var someObject = new DummyImplementingClass(); + // Assert + act.Should().Throw().WithMessage("*Expected*Other*Actual*"); + } - // Act / Assert - someObject.Should().NotBeAssignableTo(typeof(System.Collections.Generic.IList<>), "because we want to test the failure {0}", "message"); - } + [Fact] + public void When_its_own_type_instance_and_asserting_not_assignable_it_should_fail_with_a_useful_message() + { + // Arrange + var someObject = new DummyImplementingClass(); + Action act = () => someObject.Should().NotBeAssignableTo(typeof(DummyImplementingClass), "because we want to test the failure {0}", "message"); - #endregion + // Act / Assert + act.Should().Throw() + .WithMessage($"*not be assignable to {typeof(DummyImplementingClass)}*failure message*{typeof(DummyImplementingClass)} is*"); + } - #region Miscellaneous + [Fact] + public void When_its_base_type_instance_and_asserting_not_assignable_it_should_fail_with_a_useful_message() + { + // Arrange + var someObject = new DummyImplementingClass(); + Action act = () => someObject.Should().NotBeAssignableTo(typeof(DummyBaseClass), "because we want to test the failure {0}", "message"); - [Fact] - public void Should_support_chaining_constraints_with_and() - { - // Arrange - var someObject = new Exception(); - - // Act / Assert - someObject.Should() - .BeOfType() - .And - .NotBeNull(); - } + // Act / Assert + act.Should().Throw() + .WithMessage($"*not be assignable to {typeof(DummyBaseClass)}*failure message*{typeof(DummyImplementingClass)} is*"); + } - #endregion + [Fact] + public void When_an_implemented_interface_type_instance_and_asserting_not_assignable_it_should_fail_with_a_useful_message() + { + // Arrange + var someObject = new DummyImplementingClass(); + Action act = () => someObject.Should().NotBeAssignableTo(typeof(IDisposable), "because we want to test the failure {0}", "message"); - #region BeBinarySerializable + // Act / Assert + act.Should().Throw() + .WithMessage($"*not be assignable to {typeof(IDisposable)}*failure message*{typeof(DummyImplementingClass)} is*"); + } - [Fact] - public void When_an_object_is_binary_serializable_it_should_succeed() - { - // Arrange - var subject = new SerializableClass + [Fact] + public void When_an_implemented_open_generic_interface_type_instance_and_asserting_not_assignable_it_should_fail_with_a_useful_message() { - Name = "John", - Id = 2 - }; + // Arrange + var someObject = new System.Collections.Generic.List(); + Action act = () => someObject.Should().NotBeAssignableTo(typeof(System.Collections.Generic.IList<>), "because we want to test the failure {0}", "message"); - // Act - Action act = () => subject.Should().BeBinarySerializable(); + // Act / Assert + act.Should().Throw() + .WithMessage($"*not be assignable to {typeof(System.Collections.Generic.IList<>)}*failure message*{typeof(System.Collections.Generic.List)} is*"); + } - // Assert - act.Should().NotThrow(); - } + [Fact] + public void When_a_null_instance_is_asserted_to_not_be_assignable_it_should_fail_with_a_descriptive_message() + { + // Arrange + object someObject = null; + + // Act + Action act = () => + { + using var _ = new AssertionScope(); + someObject.Should().NotBeAssignableTo(typeof(DateTime), "because we want to test the failure {0}", "message"); + }; + + // Assert + act.Should().Throw() + .WithMessage($"*not be assignable to {typeof(DateTime)}*failure message*found *"); + } - [Fact] - public void When_an_object_is_binary_serializable_with_non_serializable_members_it_should_succeed() - { - // Arrange - var subject = new SerializableClassWithNonSerializableMember() + [Fact] + public void When_an_unrelated_type_instance_and_asserting_not_assignable_it_should_succeed() { - Name = "John", - NonSerializable = "Nonserializable value" - }; + // Arrange + var someObject = new DummyImplementingClass(); - // Act - Action act = () => subject.Should().BeBinarySerializable(options => - options.Excluding(s => s.NonSerializable)); + // Act / Assert + someObject.Should().NotBeAssignableTo(typeof(DateTime), "because we want to test the failure {0}", "message"); + } - // Assert - act.Should().NotThrow(); + [Fact] + public void When_unrelated_to_open_generic_type_and_asserting_not_assignable_it_should_succeed() + { + // Arrange + var someObject = new DummyImplementingClass(); + + // Act / Assert + someObject.Should().NotBeAssignableTo(typeof(System.Collections.Generic.IList<>), "because we want to test the failure {0}", "message"); + } } - [Fact] - public void When_injecting_null_options_it_should_throw() + public class Miscellaneous { - // Arrange - var subject = new SerializableClassWithNonSerializableMember(); - - // Act - Action act = () => subject.Should().BeBinarySerializable(options: null); - - // Assert - act.Should().ThrowExactly() - .WithParameterName("options"); + [Fact] + public void Should_support_chaining_constraints_with_and() + { + // Arrange + var someObject = new Exception(); + + // Act / Assert + someObject.Should() + .BeOfType() + .And + .NotBeNull(); + } } - [Fact] - public void When_an_object_is_not_binary_serializable_it_should_fail() + public class BeBinarySerializable { - // Arrange - var subject = new UnserializableClass + [Fact] + public void When_an_object_is_binary_serializable_it_should_succeed() { - Name = "John" - }; - - // Act - Action act = () => subject.Should().BeBinarySerializable("we need to store it on {0}", "disk"); + // Arrange + var subject = new SerializableClass + { + Name = "John", + Id = 2 + }; + + // Act + Action act = () => subject.Should().BeBinarySerializable(); + + // Assert + act.Should().NotThrow(); + } - // Assert - act.Should().Throw() - .WithMessage("*to be serializable because we need to store it on disk, but serialization failed with:*UnserializableClass*"); - } + [Fact] + public void When_an_object_is_binary_serializable_with_non_serializable_members_it_should_succeed() + { + // Arrange + var subject = new SerializableClassWithNonSerializableMember() + { + Name = "John", + NonSerializable = "Nonserializable value" + }; + + // Act + Action act = () => subject.Should().BeBinarySerializable(options => + options.Excluding(s => s.NonSerializable)); + + // Assert + act.Should().NotThrow(); + } - [Fact] - public void When_an_object_is_binary_serializable_but_not_deserializable_it_should_fail() - { - // Arrange - var subject = new BinarySerializableClassMissingDeserializationConstructor + [Fact] + public void When_injecting_null_options_it_should_throw() { - Name = "John", - BirthDay = 20.September(1973) - }; + // Arrange + var subject = new SerializableClassWithNonSerializableMember(); - // Act - Action act = () => subject.Should().BeBinarySerializable(); + // Act + Action act = () => subject.Should().BeBinarySerializable(options: null); - // Assert - act.Should().Throw() - .WithMessage("*to be serializable, but serialization failed with:*BinarySerializableClassMissingDeserializationConstructor*"); - } + // Assert + act.Should().ThrowExactly() + .WithParameterName("options"); + } - [Fact] - public void When_an_object_is_binary_serializable_but_doesnt_restore_all_properties_it_should_fail() - { - // Arrange - var subject = new BinarySerializableClassNotRestoringAllProperties + [Fact] + public void When_an_object_is_not_binary_serializable_it_should_fail() { - Name = "John", - BirthDay = 20.September(1973) - }; + // Arrange + var subject = new UnserializableClass + { + Name = "John" + }; + + // Act + Action act = () => subject.Should().BeBinarySerializable("we need to store it on {0}", "disk"); + + // Assert + act.Should().Throw() + .WithMessage("*to be serializable because we need to store it on disk, but serialization failed with:*UnserializableClass*"); + } - // Act - Action act = () => subject.Should().BeBinarySerializable(); + [Fact] + public void When_an_object_is_binary_serializable_but_not_deserializable_it_should_fail() + { + // Arrange + var subject = new BinarySerializableClassMissingDeserializationConstructor + { + Name = "John", + BirthDay = 20.September(1973) + }; + + // Act + Action act = () => subject.Should().BeBinarySerializable(); + + // Assert + act.Should().Throw() + .WithMessage("*to be serializable, but serialization failed with:*BinarySerializableClassMissingDeserializationConstructor*"); + } - // Assert - act.Should().Throw() - .WithMessage("*to be serializable, but serialization failed with:*subject.Name*to be*"); - } + [Fact] + public void When_an_object_is_binary_serializable_but_doesnt_restore_all_properties_it_should_fail() + { + // Arrange + var subject = new BinarySerializableClassNotRestoringAllProperties + { + Name = "John", + BirthDay = 20.September(1973) + }; + + // Act + Action act = () => subject.Should().BeBinarySerializable(); + + // Assert + act.Should().Throw() + .WithMessage("*to be serializable, but serialization failed with:*subject.Name*to be*"); + } - [Fact] - public void When_a_system_exception_is_asserted_to_be_serializable_it_should_compare_its_fields_and_properties() - { - // Arrange - var subject = new Exception("some error"); + [Fact] + public void When_a_system_exception_is_asserted_to_be_serializable_it_should_compare_its_fields_and_properties() + { + // Arrange + var subject = new Exception("some error"); - // Act - Action act = () => subject.Should().BeBinarySerializable(); + // Act + Action act = () => subject.Should().BeBinarySerializable(); - // Assert - act.Should().NotThrow(); + // Assert + act.Should().NotThrow(); + } } - + internal class UnserializableClass { public string Name { get; set; } @@ -1013,61 +1017,60 @@ public void GetObjectData(SerializationInfo info, StreamingContext context) info.AddValue("BirthDay", BirthDay); } } - - #endregion - - #region BeXmlSerializable - - [Fact] - public void When_an_object_is_xml_serializable_it_should_succeed() + + public class BeXmlSerializable { - // Arrange - var subject = new XmlSerializableClass + [Fact] + public void When_an_object_is_xml_serializable_it_should_succeed() { - Name = "John", - Id = 1 - }; - - // Act - Action act = () => subject.Should().BeXmlSerializable(); - - // Assert - act.Should().NotThrow(); - } + // Arrange + var subject = new XmlSerializableClass + { + Name = "John", + Id = 1 + }; + + // Act + Action act = () => subject.Should().BeXmlSerializable(); + + // Assert + act.Should().NotThrow(); + } - [Fact] - public void When_an_object_is_not_xml_serializable_it_should_fail() - { - // Arrange - var subject = new NonPublicClass + [Fact] + public void When_an_object_is_not_xml_serializable_it_should_fail() { - Name = "John" - }; - - // Act - Action act = () => subject.Should().BeXmlSerializable("we need to store it on {0}", "disk"); - - // Assert - act.Should().Throw() - .WithMessage("*to be serializable because we need to store it on disk, but serialization failed with:*NonPublicClass*"); - } + // Arrange + var subject = new NonPublicClass + { + Name = "John" + }; + + // Act + Action act = () => subject.Should().BeXmlSerializable("we need to store it on {0}", "disk"); + + // Assert + act.Should().Throw() + .WithMessage("*to be serializable because we need to store it on disk, but serialization failed with:*NonPublicClass*"); + } - [Fact] - public void When_an_object_is_xml_serializable_but_doesnt_restore_all_properties_it_should_fail() - { - // Arrange - var subject = new XmlSerializableClassNotRestoringAllProperties + [Fact] + public void When_an_object_is_xml_serializable_but_doesnt_restore_all_properties_it_should_fail() { - Name = "John", - BirthDay = 20.September(1973) - }; - - // Act - Action act = () => subject.Should().BeXmlSerializable(); - - // Assert - act.Should().Throw() - .WithMessage("*to be serializable, but serialization failed with:*Name*to be*"); + // Arrange + var subject = new XmlSerializableClassNotRestoringAllProperties + { + Name = "John", + BirthDay = 20.September(1973) + }; + + // Act + Action act = () => subject.Should().BeXmlSerializable(); + + // Assert + act.Should().Throw() + .WithMessage("*to be serializable, but serialization failed with:*Name*to be*"); + } } internal class NonPublicClass @@ -1104,91 +1107,90 @@ public void WriteXml(XmlWriter writer) } } - #endregion - - #region BeDataContractSerializable - - [Fact] - public void When_an_object_is_data_contract_serializable_it_should_succeed() + public class BeDataContractSerializable { - // Arrange - var subject = new DataContractSerializableClass + [Fact] + public void When_an_object_is_data_contract_serializable_it_should_succeed() { - Name = "John", - Id = 1 - }; - - // Act - Action act = () => subject.Should().BeDataContractSerializable(); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_an_object_is_not_data_contract_serializable_it_should_fail() - { - // Arrange - var subject = new NonDataContractSerializableClass(); - - // Act - Action act = () => subject.Should().BeDataContractSerializable("we need to store it on {0}", "disk"); - - // Assert - act - .Should().Throw() - .WithMessage("*we need to store it on disk*EnumMemberAttribute*"); - } + // Arrange + var subject = new DataContractSerializableClass + { + Name = "John", + Id = 1 + }; + + // Act + Action act = () => subject.Should().BeDataContractSerializable(); + + // Assert + act.Should().NotThrow(); + } - [Fact] - public void When_an_object_is_data_contract_serializable_but_doesnt_restore_all_properties_it_should_fail() - { - // Arrange - var subject = new DataContractSerializableClassNotRestoringAllProperties + [Fact] + public void When_an_object_is_not_data_contract_serializable_it_should_fail() { - Name = "John", - BirthDay = 20.September(1973) - }; + // Arrange + var subject = new NonDataContractSerializableClass(); - // Act - Action act = () => subject.Should().BeDataContractSerializable(); + // Act + Action act = () => subject.Should().BeDataContractSerializable("we need to store it on {0}", "disk"); - // Assert - act.Should().Throw() - .WithMessage("*to be serializable, but serialization failed with:*property subject.Name*to be*"); - } + // Assert + act + .Should().Throw() + .WithMessage("*we need to store it on disk*EnumMemberAttribute*"); + } - [Fact] - public void When_a_data_contract_serializable_object_doesnt_restore_an_ignored_property_it_should_succeed() - { - // Arrange - var subject = new DataContractSerializableClassNotRestoringAllProperties + [Fact] + public void When_an_object_is_data_contract_serializable_but_doesnt_restore_all_properties_it_should_fail() { - Name = "John", - BirthDay = 20.September(1973) - }; - - // Act - Action act = () => subject.Should().BeDataContractSerializable( - options => options.Excluding(x => x.Name)); + // Arrange + var subject = new DataContractSerializableClassNotRestoringAllProperties + { + Name = "John", + BirthDay = 20.September(1973) + }; + + // Act + Action act = () => subject.Should().BeDataContractSerializable(); + + // Assert + act.Should().Throw() + .WithMessage("*to be serializable, but serialization failed with:*property subject.Name*to be*"); + } - // Assert - act.Should().NotThrow(); - } + [Fact] + public void When_a_data_contract_serializable_object_doesnt_restore_an_ignored_property_it_should_succeed() + { + // Arrange + var subject = new DataContractSerializableClassNotRestoringAllProperties + { + Name = "John", + BirthDay = 20.September(1973) + }; + + // Act + Action act = () => subject.Should().BeDataContractSerializable( + options => options.Excluding(x => x.Name)); + + // Assert + act.Should().NotThrow(); + } - [Fact] - public void When_injecting_null_options_to_BeDataContractSerializable_it_should_throw() - { - // Arrange - var subject = new DataContractSerializableClassNotRestoringAllProperties(); + [Fact] + public void When_injecting_null_options_to_BeDataContractSerializable_it_should_throw() + { + // Arrange + var subject = new DataContractSerializableClassNotRestoringAllProperties(); - // Act - Action act = () => subject.Should().BeDataContractSerializable( - options: null); + // Act + Action act = () => subject.Should().BeDataContractSerializable( + options: null); - // Assert - act.Should().ThrowExactly() - .WithParameterName("options"); + // Assert + act.Should().ThrowExactly() + .WithParameterName("options"); + } } public enum Color @@ -1217,9 +1219,6 @@ public class DataContractSerializableClassNotRestoringAllProperties [DataMember] public DateTime BirthDay { get; set; } } - - #endregion - } internal class DummyBaseClass