0

How to exclude a JSON property from comparison in FluentAssertions.Json?

JToken token, expectedJson;
token.Should().BeEquivalentTo(expectedJson);
{
  "property1":"value1",
  "property2":"value2",
  "property3":"value3"
}

I want to exclude property with name "property2" from the comparison. How is it possible?

JHBonarius
  • 10,824
  • 3
  • 22
  • 41
Andrej B.
  • 187
  • 1
  • 5
  • 14
  • Did you read the [readme](https://github.com/fluentassertions/fluentassertions.json)? You should probably configure some `IJsonAssertionOptions<>` – JHBonarius Oct 31 '22 at 14:43
  • 1
    You mean something like `token.Should().BeEquivilentTo(expectedJson, o=>o.Excluding(p=>p.Property2));` – Neil Oct 31 '22 at 14:59
  • There is no Excluding method in IJsonAssertionOptions interface. – Andrej B. Nov 01 '22 at 14:24
  • @AndrejB. Do you have Exclude ? something like token.Should().BeEquivalentTo(expectedJson, options => options.For(o => o.Property1).Exclude(o => o.Property2)); – tanuk Nov 02 '22 at 10:57
  • I found that I have a restriction by using net462. FluentAssertions for net462 does not have overload of BeEquivilentTo with the options. So, I fixed it with another approach. – Andrej B. Nov 02 '22 at 12:40

2 Answers2

0

I resolved it by preprocessing of token and expectedJson.

JToken token, expectedJson;
((JValue)expectedJson.SelectToken("property2")).Value = null;
((JValue)token.SelectToken("property2")).Value = null;
token.Should().BeEquivalentTo(expectedJson);
Andrej B.
  • 187
  • 1
  • 5
  • 14
-1

You could test the properties directly and not the whole object.

tanuk
  • 498
  • 9
  • 10
  • This is a poor answer. Checking properties individually is not very good future proofing. – Neil Oct 31 '22 at 14:59