Sometimes I have to assert that two lists have the same items. With fluent assertions this can be done like this:
class MyObject { public string MyString {get; set;} }
var o1 = new MyObject { MyString = "1 " }
list1.Add(o1);
var o2 = new MyObject { MyString = "1" }
list2.Add(o2);
list1.Should().BeEquivalentTo(list2)
But sometimes I want a specific property to be compared in a different way, like this:
list1.Should().BeEquivalentTo(list2, options => options
.Using<string>(context => context.Subject.TrimEnd().Should().Be(context.Expectation))
.When<string>( ??????? ));
I have tried:
it => it.SelectedMemberInfo.Name == PropertyNameHere
But SelectedMemberInfo can be null and it throws an exception when null and I dont know if I'm calling it the correct way.
Update 1: Tried, but null reference exception:
options => options
.Using<DateTime>(it => it.Subject.Should().BeCloseTo(DateTime.Now, TimeSpan.FromMinutes(1)))
.When(it =>
it != null
&& it.SelectedMemberInfo != null
&& it.SelectedMemberInfo.Name == nameof(Y.X)));