Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ContainSubtree constraint #6

Merged
merged 4 commits into from
Jan 12, 2018
Merged

ContainSubtree constraint #6

merged 4 commits into from
Jan 12, 2018

Conversation

BalassaMarton
Copy link

@BalassaMarton BalassaMarton commented Aug 25, 2017

This constraint will match a JSON document against a subtree. This is useful when verifying JSON data, and multiple but not all properties of an object must be compared against known values.
Example: I am testing some CRUD web service, have a response like this:

{
  success: true,
  data: {
    id: 123,
    name: 'Foo',
    type: 'my-type',
    someOtherProperty: 'asd'
  }
}

...and I want to assert that the request succeeded and the returned object has type my-type with name Foo. The other properties are irrelevant and/or unknown. Without this assertion, I'd have to write something like this:

response["success"].Should().HaveValue(true);
response["data"]["type"].Should().HaveValue("my-type");
response["data"]["name"].Should().HaveValue("Foo");

Yuck! The same test can be done with a single call using ContainSubtree:

response.Should().ContainSubtree(JToken.Parse(" { success: true, data: { type: 'my-type', name: 'Foo' }  } "));

When using with arrays, the assertion is that the JSON array under testing must contain a matching element for each of the elements in the subtree array, eg. this should fail:

var json = JToken.Parse("[ { name: 'Alpha' }, { name: 'Bravo' }, { name: 'Charlie' } ]");
json.Should().ContainSubtree(JToken.Parse("[ { name: 'Alpha' }, { name: 'Delta' } ]"));

@dennisdoomen
Copy link
Member

Sorry, I missed this PR. Would you be willing to rebase on the release-5.0 hot-fix branch?

@BalassaMarton
Copy link
Author

Hi, sure, as soon as I learn how to do that :)

@dennisdoomen
Copy link
Member

@BalassaMarton
Copy link
Author

I did a rebase althouth I'm not sure it worked. Can you look at it? I also commited the nuspec, please ignore it when merging.

@dennisdoomen dennisdoomen merged commit 57d8779 into fluentassertions:master Jan 12, 2018
dennisdoomen added a commit to dennisdoomen/fluentassertions.json that referenced this pull request Jan 13, 2018
dennisdoomen added a commit that referenced this pull request Jan 13, 2018
@dennisdoomen
Copy link
Member

Apparently you forgot to change the target branch to release 5.0. I didn't see that and accidentally merged this change. I'm currently merging 5.0 to master, so I'll ping you to re-apply the change later on

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants