PSMatcher is a test utility, that lets you easier test responses and json, when some part of the response is something out of your control (autogenerated id, guid, datetime etc).
It's here on the PowerShell Gallery
This PowerShell has been tested on Windows in both v5.1 and v6.0
Import-Module -Name "$PSScriptRoot\..\PSMatcher.psm1" -Force
$actual = @"
{
"id" : "5a645a20-5225-431b-8c62-031b87f58b73",
"subnode" : {
"city" : "NY",
"zipCode" : "80-000",
"meta" : {
"name" : "foobar",
"shipping": 99.99,
"enabled" : false,
"_link" : "http://example.com?page=2",
"_something" : null,
"_arr" : [1, 2, 3],
"_date" : "2018-01-01"
}
}
}
"@
$expected = @"
{
"id" : "@guid@",
"subnode" : {
"city" : "NY",
"zipCode" : "@string@",
"meta" : {
"name" : "@[email protected]('bar')",
"shipping": "@double@",
"enabled" : "@bool@",
"_link" : "@any@",
"_something" : "@null@",
"_arr" : [1, 2, 3],
"_date" : "@[email protected]()"
}
}
}
"@
# True Result
Test-Json -Value $actual -Reference $expected
# False Result
Test-Json -Value $actual -Reference '{}'
Successful ErrorMessage Result
---------- ------------ ------
True NMatcher.Matching.Result
False Expected value did not appear at path id. NMatcher.Matching.Result
The matcher also checks if elements are missing or if too many are supplied.
Stay Tuned: Examples will be added showing how well this works with PowerShell objects and Hashtables.
- @string@
- @int@
- @double@
- @bool@
- @null@
- @any@
- @guid@
This wraps NMatcher. Check out that readme for more detail.