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

MatchOptions.IgnoreFields() throws SnapshotFieldException #132

Open
lamLeX opened this issue Jun 18, 2021 · 3 comments
Open

MatchOptions.IgnoreFields() throws SnapshotFieldException #132

lamLeX opened this issue Jun 18, 2021 · 3 comments
Labels
bug Something isn't working
Milestone

Comments

@lamLeX
Copy link

lamLeX commented Jun 18, 2021

Describe the bug
I'm trying to set the ignorefields with this syntax

result.MatchSnapshot(matchOptions=>matchOptions.IgnoreFields("$.[?(@.Result != null)].Result.Id"))

it failed on certain test cases

This case passes

[
  {
    "Error": "",
    "IsEmpty": false,
    "Result": {
      "Id": "cabcf393-e26e-4adb-af69-7125ca6774e8",
      // additional properties here abbreviated for concise
    },
    "ReplyCase": "Result"
  },
  {
    "Error": "",
    "IsEmpty": true,
    "Result": null,
    "ReplyCase": "IsEmpty"
  }
]

This case also passes

[
  {
    "Error": "",
    "IsEmpty": false,
    "Result": {
      "Id": "a9039bbb-3e65-485b-85ae-b752a8169401",
      // additional properties here abbreviated for concise
    },
    "ReplyCase": "Result"
  },
  {
    "Error": "",
    "IsEmpty": false,
    "Result": {
      "Id": "58e3b1fa-3d2b-4521-a241-6f58948ab411",
      // additional properties here abbreviated for concise
    },
    "ReplyCase": "Result"
  }
]

However, this case will fail

[
  {
    "Error": "",
    "IsEmpty": true,
    "Result": null,
    "ReplyCase": "IsEmpty"
  }
]

with this exception

Snapshooter.Exceptions.SnapshotFieldException
The fields of '$.[?(@.Result != null)].Result.Id' of the compare context caused an error. No fields of the path '$.[?(@.Result != null)].Result.Id' could not be found.

Expected behavior
all presented test cases should pass

@lamLeX
Copy link
Author

lamLeX commented Jun 19, 2021

Should we relax this check

if (fields == null || fields.Count() == 0)
{
throw new SnapshotFieldException(
$"No fields of the path '{fieldPath}' could not be found.");
}

And return Empty array instead?

T[] fieldValues = (fields?.Select(f => ConvertToType<T>(f)) ?? Enumerable.Empty<T>).ToArray();

@woeterman94
Copy link

Is it possible to ignore fields if they don't exist? We have some fields that are not always in the result.

@nscheibe nscheibe added this to the 1.0.0 milestone Jul 15, 2021
@nscheibe nscheibe added enhancement New feature or request bug Something isn't working and removed enhancement New feature or request labels Jul 15, 2021
@thomasrosdahl
Copy link

It's also possible to add an extension method like this:

public static class MatchOptionsExtensions
    {
        public static MatchOptions IgnoreFieldsWhen(this MatchOptions matchOptions, bool condition, string fieldsPath)
        {
            if (condition)
            {
                matchOptions.IgnoreFields(fieldsPath);
            }

            return matchOptions;
        }
    }

Then .IgnoreFieldsWhen(data.Result != null, "$.Result.Id")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants