Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Variables inside the assertion script block #18

Open
DexterPOSH opened this issue Jul 14, 2016 · 1 comment
Open

Variables inside the assertion script block #18

DexterPOSH opened this issue Jul 14, 2016 · 1 comment

Comments

@DexterPOSH
Copy link

DexterPOSH commented Jul 14, 2016

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
Copy link
Contributor

cdhunt commented Jul 14, 2016

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants