diff --git a/Src/FluentAssertions/Equivalency/GenericDictionaryEquivalencyStep.cs b/Src/FluentAssertions/Equivalency/GenericDictionaryEquivalencyStep.cs index ff1b181dd5..7c66ba7717 100644 --- a/Src/FluentAssertions/Equivalency/GenericDictionaryEquivalencyStep.cs +++ b/Src/FluentAssertions/Equivalency/GenericDictionaryEquivalencyStep.cs @@ -44,11 +44,19 @@ private static bool PreconditionsAreMet(IEquivalencyValidationContext context, I Type expectationType = config.GetExpectationType(context); return AssertImplementsOnlyOneDictionaryInterface(context.Expectation) + && AssertSubjectIsNotNull(context.Subject) && AssertExpectationIsNotNull(context.Subject, context.Expectation) && AssertIsCompatiblyTypedDictionary(expectationType, context.Subject) && AssertSameLength(context.Subject, expectationType, context.Expectation); } + private static bool AssertSubjectIsNotNull(object subject) + { + return AssertionScope.Current + .ForCondition(!(subject is null)) + .FailWith("Expected {context:Subject} not to be {0}.", new object[] { null }); + } + private static bool AssertExpectationIsNotNull(object subject, object expectation) { return AssertionScope.Current diff --git a/Tests/Shared.Specs/DictionaryEquivalencySpecs.cs b/Tests/Shared.Specs/DictionaryEquivalencySpecs.cs index 74961e5909..99fcba6664 100644 --- a/Tests/Shared.Specs/DictionaryEquivalencySpecs.cs +++ b/Tests/Shared.Specs/DictionaryEquivalencySpecs.cs @@ -377,6 +377,50 @@ public void When_a_read_only_dictionary_does_not_match_the_expectation_it_should act.Should().Throw().WithMessage("Expected item[0]*Value3*Value2*"); } + [Fact] + // #930 + public void When_a_dictionary_is_compared_to_null_it_should_not_throw_a_NullReferenceException() + { + + //----------------------------------------------------------------------------------------------------------- + // Arrange + //----------------------------------------------------------------------------------------------------------- + Dictionary subject = null; + Dictionary expectation = new Dictionary(); + + //----------------------------------------------------------------------------------------------------------- + // Act + //----------------------------------------------------------------------------------------------------------- + Action act = () => subject.Should().BeEquivalentTo(expectation); + + //----------------------------------------------------------------------------------------------------------- + // Assert + //----------------------------------------------------------------------------------------------------------- + act.Should().Throw(); + } + + [Fact] + // #930 + public void When_a_null_dictionary_is_compared_to_null_it_should_not_throw() + { + + //----------------------------------------------------------------------------------------------------------- + // Arrange + //----------------------------------------------------------------------------------------------------------- + Dictionary subject = null; + Dictionary expectation = null; + + //----------------------------------------------------------------------------------------------------------- + // Act + //----------------------------------------------------------------------------------------------------------- + Action act = () => subject.Should().BeEquivalentTo(expectation); + + //----------------------------------------------------------------------------------------------------------- + // Assert + //----------------------------------------------------------------------------------------------------------- + act.Should().NotThrow(); + } + [Fact] public void When_a_dictionary_property_is_detected_it_should_ignore_the_order_of_the_pairs