0

I have two records:

public record A(string AId, IReadOnlyList<B> list)

public record B(string BId, string Color)

I want to use fluent assertions to compare collections of instances of A, while excluding the proberty BId

e.g: Let's say I have

FirstCollection = {{AId:"AId1", BId:"BId1 ,Color:"Red"} ,{AId:"AId2", BId:"BId2", Color:"Red"}}

SecondCollection = {{AId:"AId1", BId:"BId3", Color:"Red"} ,{AId:"AId2", BId:"BId4", Color:"Red"}}

Asserting the following two collections should pass.

omar kh
  • 11
  • 4
  • Can you give an example? If I undertand you correctly, you want to make sure that two instances have the same string AId, and all elements of list have the same Color, but may have different BIds? – SomeBody Feb 14 '22 at 09:37
  • Yes, exactly. Added an example if that helps – omar kh Feb 14 '22 at 09:58
  • Please include a [minimal, complete and verifiable example](https://stackoverflow.com/help/minimal-reproducible-example). It's difficult to help you, if we have to guess your code. – Jonas Nyrup Feb 15 '22 at 21:49

1 Answers1

0

You can use the Excluding method, something like this:

expected.ShouldBeEquivalentTo(actual, options => options.Excluding(s => 
   (s.RuntimeType == typeof(B)) && (s.PropertyPath.EndsWith("BId")));

https://github.com/fluentassertions/fluentassertions/issues/500

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
  • Didn't work. I think it might have to do with the fact that I am asserting a collection of A's. So the configuration here were added to instance's of A which does not have BId. – omar kh Feb 14 '22 at 09:40
  • @omarkh You need to change this line based on what you are exactly looking for, it is hard to provide a working answer without seeing your code. – Salah Akbari Feb 14 '22 at 09:42
  • Sorry if I wasn't clear. I want to compare two collections of instances of A's. where each instance's of A should have the same AId and color but does not have to be the same BId. Added an example if that helps – omar kh Feb 14 '22 at 10:00
  • @omarkh Please try this and let me know if it works `expected.ShouldBeEquivalentTo(actual, options => options.Excluding(s => (Regex.IsMatch(s.SelectedMemberPath, "list\\[.+\\].BId"));` – Salah Akbari Feb 14 '22 at 10:11
  • Didn't work also. Tried to override equal on class B without comparing the BId's and the assertion worked. I would say the Excluding method didn't work as expected in your code – omar kh Feb 14 '22 at 10:25
  • Are you familiar with a way where I can override the B class equal's for the tests only? – omar kh Feb 14 '22 at 10:29