Variables inside the assertion script block
DexterPOSH opened this issue · 1 comments
DexterPOSH commented
Since the Assertion in the PoshSpec is placed inside a scriptblock, how does one reference variables in it e.g below works fine when the assertion is hardcoded -:
Describe test {
Service Bits Status {Should be 'Running'}
}
But when a variable is placed in the assertion, it can't see them e.g.
Describe test {
$DesiredStatus = 'Running'
Service Bits Status {Should be $DesiredStatus}
}
Above gives me the error -:
Describing test
[-] Service property 'Status' for 'Bits' Should be $DesiredStatus 123ms
Expected: {}
But was: {Running}
1: Get-Service -Name 'Bits' | Select-Object -ExpandProperty 'Status' | Should be $Desir
edStatus
at <ScriptBlock>, <No file>: line 1
at <ScriptBlock>, C:\Users\Deepak_Dhami\Documents\GitHub\poshspec\Private\Invoke-Poshsp
ecExpression.ps1: line 12
The idea is to use a CSV file as a template for all the service and their compliant status.
How do I do something like this with PoshSpec ?
Describe 'IT Compliance for Services' {
$ObjectFromCSV = @([PSCustomObject]@{
Name='Bits';
Status='Stopped'
})
$ObjectFromCSV.Foreach({
Service $PSItem.Name Status {Should be $PSItem.Status}
})
}
cdhunt commented
I would say that is an architectural limitation of PoshSpec that will not be addressed in the short-term. I think there are two work arounds: you can create a loop for each State you want to test and hard code the test value; or, you can declare a variable with Global
scope.