Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
Added Hash Option Explanation
  • Loading branch information
nscheibe authored Nov 7, 2022
1 parent 5d271e7 commit 84cef51
Showing 1 changed file with 52 additions and 2 deletions.
54 changes: 52 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down Expand Up @@ -157,7 +157,57 @@ Option 2: Use the default ignore match option 'IgnoreFields(**.<fieldName>)' wit
Snapshot.Match<Person>(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>(person, matchOptions => matchOptions.HashField("Thumbnail.Data"));

// Hash the field 'Data' of the first thumbnail in the 'Thumbnails' array of the image
Snapshot.Match<Person>(person, matchOptions => matchOptions.HashField("Thumbnails[0].Data"));

// Ignores the field 'Data' of all 'Thumbnails' nodes of the image
Snapshot.Match<Person>(person, matchOptions => matchOptions.HashField("Thumbnails[*].Data"));

// Ignores all fields with name 'Data'
Snapshot.Match<Person>(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.

Expand Down

0 comments on commit 84cef51

Please sign in to comment.