diff --git a/README.md b/README.md index 9b67a83..4fa52af 100644 --- a/README.md +++ b/README.md @@ -102,7 +102,7 @@ For NUnit we will support the Assert.That syntax (Coming soon): ## Features -### Ignore Fields in Snapshots Matches +### Ignore Fields in Snapshots If some fields in your snapshot shall be ignored during snapshot assertion, then the following ignore options can be used: @@ -157,7 +157,57 @@ Option 2: Use the default ignore match option 'IgnoreFields(**.)' wit Snapshot.Match(person, matchOptions => matchOptions.IgnoreFields("**.Id")); ``` -### Assert Fields in Snapshots Matches +### Hash Fields in Snapshots + +If some fields of our snapshot are too big, for example a binary field with a lot of data, then we can use the HashField option. +The HashField option creates a Hash of the field value and therefore each time only the hash is compared. +If there is a change in the field value, then the snapshot match will fail. + +```csharp +[Fact] +public void ImageSnapshot_HashImageBinary() +{ + // arrange + var serviceClient = new ServiceClient(); + + // act + TestImage image = serviceClient.CreateMonaLisaImage(); + + // assert + Snapshot.Match(image, matchOptions => matchOptions.HashField("Data")); +} +``` +Example Snapshot with Hash +```json +{ + "Id": 3450987, + "OwnerId": "0680faef-6e89-4d52-bad8-291053c66696", + "Name": "Mona Lisa", + "CreationDate": "2020-11-10T21:23:09.036+01:00", + "Price": 951868484.345, + "Data": "m+sQR9KG9WpgYoQiRASPkt9FLJOLsjK86UuiXKVRzas=" +} +``` + +The field(s) to hash can be located via JsonPath or via field name. + +Hash Field Examples: + +```csharp +// Hash the field 'Data' of the child node 'Thumbnail' of the person +Snapshot.Match(person, matchOptions => matchOptions.HashField("Thumbnail.Data")); + +// Hash the field 'Data' of the first thumbnail in the 'Thumbnails' array of the image +Snapshot.Match(person, matchOptions => matchOptions.HashField("Thumbnails[0].Data")); + +// Ignores the field 'Data' of all 'Thumbnails' nodes of the image +Snapshot.Match(person, matchOptions => matchOptions.HashField("Thumbnails[*].Data")); + +// Ignores all fields with name 'Data' +Snapshot.Match(person, matchOptions => matchOptions.HashField("**.Data")); +``` + +### Assert Fields in Snapshots Sometimes there are fields in a snapshot, which you want to assert separately against another value.