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

Problem with BeforeAll and AfterAll when supplying scriptblock #300

Closed
JonSutton opened this issue Apr 1, 2015 · 5 comments
Closed

Problem with BeforeAll and AfterAll when supplying scriptblock #300

JonSutton opened this issue Apr 1, 2015 · 5 comments
Assignees

Comments

@JonSutton
Copy link

BeforeAll and AfterAll do not work with script blocks (BeforeEach + AfterEach do not have this issue)

I tried this:

    $beforeAllScriptBlock = [ScriptBlock]::Create("$PSScriptRoot\Tests\TestSetup.ps1")
    BeforeAll { &$beforeAllScriptBlock }

but this failed with the message:

Invoke-Blocks : The expression after '&' in a pipeline element produced an object that was not valid. It must result in a command name, a script block, or a CommandInfo object.
At...

What I really wanted to be able to do was this:

Describe "foo" {
    Context "bar" {
        BeforeAll {
            &"$PSScriptRoot\Tests\TestSetup.ps1"
        }
        It "Tests Something" {
            ........
        }
    }
}

the above fails to insert $PSScriptRoot (it's coming out blank in the error message)

I've tried a the [ScriptBlock]::Create approach for BeforeEach and it works just fine.

I've added to the set of tests here that pass on Before/AfterEach and fail for Before/AfterAll: JonSutton@6edfa373fa3cae0940aee4c747e86add87734dea

Thanks!

@dlwyatt
Copy link
Member

dlwyatt commented Apr 1, 2015

Will investigate.

@dlwyatt dlwyatt self-assigned this Apr 1, 2015
@nohwnd
Copy link
Member

nohwnd commented Apr 1, 2015

In the Invoke-TestGroupSetupBlocks the scriptblockis moved to different scope. The content of the scriptblock is correctly passed (for example: &$scriptBlock), but the variable containing the script is not passed., but the variable is not assigned yet. Hence you get the same error as if you'd do &$null.

@nohwnd
Copy link
Member

nohwnd commented Apr 1, 2015

The BeforeAll is running BEFORE the Describe. So at that point the $scriptBlock is not defined. You need to put the assignment before the Describe.

Describe 'error' {   
    BeforeAll {
        &$a
     }
    It 'empty' {
        0 | should Be 0
    }
}

$a = [scriptBlock]::Create("Write-Host 'a'")
Describe 'no error' {

    BeforeAll {
        &$a
     }
    It 'empty' {
        0 | should Be 0
    }
}

Output:

Describing error
[113,5: Write-Error] The expression after '&' in a pipeline element produced an object that was not valid. It must result in a command name, a script block, or a CommandInfo object.
 [+] empty 680ms
Describing no error
a
 [+] empty 97m

@dlwyatt
Copy link
Member

dlwyatt commented Apr 1, 2015

Makes sense. Also, $PSScriptRoot is probably not working because Pester is creating an unbound script block in memory (no longer tied to the Tests.ps1 file). You can fix that by assigning another variable the value of $PSScriptRoot in your tests.ps1 file, then refer to that in BeforeAll / etc. (This is similar to how you avoid having other automatic variables such as $_ "hijacked" by later code.)

$scriptRoot = $PSScriptRoot

Describe 'Whatever' {
    BeforeAll {
        & "$scriptRoot\SomeScript.ps1"
    }
}

@JonSutton
Copy link
Author

Thanks for the great + speedy replies, realising that the BeforeAll was running before the contents of the describe was the key. Also thanks for the tip about copying the value of $PSScriptRoot so it would be available when I wanted it!

After your wise words I'm pretty sure that this issue should be closed, so i'll do that.

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

3 participants