-
-
Notifications
You must be signed in to change notification settings - Fork 473
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
Comments
Will investigate. |
In the Invoke-TestGroupSetupBlocks the |
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.
Output:
|
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"
}
} |
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. |
BeforeAll and AfterAll do not work with script blocks (BeforeEach + AfterEach do not have this issue)
I tried this:
but this failed with the message:
What I really wanted to be able to do was this:
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!
The text was updated successfully, but these errors were encountered: