"Ns": {
"value": [
{
"Nname": "exa",
"SR": [
{
"name": "port1",
"properties": {
"description": "Allow port1",
"destinationPortRange": "1111",
"priority": 100
}
},
{
"name": "port1_0",
"properties": {
"description": "Allow port1",
"destinationPortRange": "1111",
"priority": 150
}
},
{
"name": "port2",
"properties": {
"description": "Allow 1115",
"destinationPortRange": "1115",
"priority": 100,
}
}
]
}
]
}
Want to assert the details of priority and name but was not able to do it.
Here is what I have implemented:
$Ndetails = templateProperties.parameters.Ns.value.SR
foreach ($Ndata in $Ndetails) {
$Ndata .properties.destinationPortRange |
Should -BeExactly @('1111','1111','1115')
}
How to resolve the same using Pester in PowerShell?
You don't need to use
foreach
for this. You can just useSelect-Object
for this. Assuming your JSON is as @Mark Wragg linked in the comments:Your test file should look like this:
Explanation
Your use of
foreach
was incorrect as you compared single element (also notice that I deleted unnecessary space)to the array
What you have to do is to compare array to array as in my example.