1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
|
[](https://crates.io/crates/assert-json-diff)
[](https://docs.rs/assert-json-diff)
[](https://deps.rs/repo/github/davidpdrsn/assert-json-diff)
[](https://github.com/davidpdrsn/assert-json-diff/actions)

# assert-json-diff
This crate includes macros for comparing two serializable values by diffing their JSON
representations. It is designed to give much more helpful error messages than the standard
[`assert_eq!`]. It basically does a diff of the two objects and tells you the exact
differences. This is useful when asserting that two large JSON objects are the same.
It uses the [serde] and [serde_json] to perform the serialization.
[serde]: https://crates.io/crates/serde
[serde_json]: https://crates.io/crates/serde_json
[`assert_eq!`]: https://doc.rust-lang.org/std/macro.assert_eq.html
### Partial matching
If you want to assert that one JSON value is "included" in another use
[`assert_json_include`](macro.assert_json_include.html):
```rust
use assert_json_diff::assert_json_include;
use serde_json::json;
let a = json!({
"data": {
"users": [
{
"id": 1,
"country": {
"name": "Denmark"
}
},
{
"id": 24,
"country": {
"name": "Denmark"
}
}
]
}
});
let b = json!({
"data": {
"users": [
{
"id": 1,
"country": {
"name": "Sweden"
}
},
{
"id": 2,
"country": {
"name": "Denmark"
}
}
]
}
});
assert_json_include!(actual: a, expected: b)
```
This will panic with the error message:
```
json atoms at path ".data.users[0].country.name" are not equal:
expected:
"Sweden"
actual:
"Denmark"
json atoms at path ".data.users[1].id" are not equal:
expected:
2
actual:
24
```
[`assert_json_include`](macro.assert_json_include.html) allows extra data in `actual` but not in `expected`. That is so you can verify just a part
of the JSON without having to specify the whole thing. For example this test passes:
```rust
use assert_json_diff::assert_json_include;
use serde_json::json;
assert_json_include!(
actual: json!({
"a": { "b": 1 },
}),
expected: json!({
"a": {},
})
)
```
However `expected` cannot contain additional data so this test fails:
```rust
use assert_json_diff::assert_json_include;
use serde_json::json;
assert_json_include!(
actual: json!({
"a": {},
}),
expected: json!({
"a": { "b": 1 },
})
)
```
That will print
```
json atom at path ".a.b" is missing from actual
```
### Exact matching
If you want to ensure two JSON values are *exactly* the same, use [`assert_json_eq`](macro.assert_json_eq.html).
```rust
use assert_json_diff::assert_json_eq;
use serde_json::json;
assert_json_eq!(
json!({ "a": { "b": 1 } }),
json!({ "a": {} })
)
```
This will panic with the error message:
```
json atom at path ".a.b" is missing from lhs
```
### Further customization
You can use [`assert_json_matches`] to further customize the comparison.
License: MIT
|