Access External Variable from with-in Mock Script Block (Pester)

Question:

Pretend I have a function like…

…in my Pester test script…

This doesn’t work, but you see what I’m trying to do here? I want to get the value from a local variable into a MOCK script block. I want to avoid hard coding the return value in the mock and the expected result in the it-block. Any ideas on how I can achieve this?

Answer:

Wasn’t sure if it would work as not messed with pester before but apparently it follows the same scope rules as standard PowerShell.

So $script:var = 1, and the brute-force $global:var = 1 if it doesn’t or if you need to call it from outside the script scope.

Source:

Access External Variable from with-in Mock Script Block (Pester) by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply