Skip to content

Interface with Setter Only Property leads to NullReferenceExcpetion #2832

@marcoernikms

Description

@marcoernikms

Description

When asserting an object with BeEquivalentTo and the type of that object implements an interface with a setter only property, a NullReferenceException is thrown

Reproduction Steps

[TestClass]
public class Test
{
    [TestMethod]
    public void Issue()
    {
        var a = new AClass { Value = "OtherVlaue" };
        var b = new AClass { Value = "OtherVlaue" };
        
        a.Should().BeEquivalentTo(b);
    }
    
    private class AClass : IWithSetterOnlyProperty
    {
        public string Value { private get; set; }
    }
    
    private interface IWithSetterOnlyProperty
    {
        string Value { set; }
    }
}

Expected behavior

No NullReferenceException should be thrown.

Actual behavior

A NullReferenceException is thrown by Reflectify. It looks like the issue was introduce with #2794

Regression?

No response

Known Workarounds

Adding a getter to the property solves the issue.

Configuration

No response

Other information

Call stack of the NullReferenceException:

System.NullReferenceException: Object reference not set to an instance of an object.
   at Reflectify.Reflector.AddInterfaceProperties(Type typeToReflect, MemberKind kind)
   at Reflectify.Reflector.LoadProperties(Type typeToReflect, MemberKind kind)
   at Reflectify.Reflector..ctor(Type typeToReflect, MemberKind kind)
   at Reflectify.TypeExtensions.<>c.<GetFor>b__4_0(ValueTuple`2 key)
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at Reflectify.TypeExtensions.GetFor(Type typeToReflect, MemberKind kind)
   at Reflectify.TypeExtensions.GetProperties(Type type, MemberKind kind)
   at FluentAssertions.Equivalency.Selection.AllPropertiesSelectionRule.SelectMembers(INode currentNode, IEnumerable`1 selectedMembers, MemberSelectionContext context)
   at FluentAssertions.Equivalency.Steps.StructuralEqualityEquivalencyStep.GetMembersFromExpectation(INode currentNode, Comparands comparands, IEquivalencyAssertionOptions options)
   at FluentAssertions.Equivalency.Steps.StructuralEqualityEquivalencyStep.Handle(Comparands comparands, IEquivalencyValidationContext context, IEquivalencyValidator nestedValidator)
   at FluentAssertions.Equivalency.EquivalencyValidator.TryToProveNodesAreEquivalent(Comparands comparands, IEquivalencyValidationContext context)
   at FluentAssertions.Equivalency.EquivalencyValidator.RecursivelyAssertEquality(Comparands comparands, IEquivalencyValidationContext context)
   at FluentAssertions.Equivalency.EquivalencyValidator.AssertEquality(Comparands comparands, EquivalencyValidationContext context)
   at FluentAssertions.Primitives.ObjectAssertions`2.BeEquivalentTo[TExpectation](TExpectation expectation, Func`2 config, String because, Object[] becauseArgs)
   at FluentAssertions.Primitives.ObjectAssertions`2.BeEquivalentTo[TExpectation](TExpectation expectation, String because, Object[] becauseArgs)

Are you willing to help with a pull-request?

No

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions