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

Pester tests misbehave with closures #525

Closed
lmmarsano opened this issue Apr 25, 2016 · 2 comments
Closed

Pester tests misbehave with closures #525

lmmarsano opened this issue Apr 25, 2016 · 2 comments

Comments

@lmmarsano
Copy link

See lmmarsano/pester-closure-issue for a unit test demonstrating the problem.

Something strange goes on with a closure's name bindings in unit tests.
This code

Set-StrictMode -Version latest
function Outer {
    {
        Inner
    }.GetNewClosure()
}
function Inner {
    'call Inner'
}

and this unit test

Describe 'function in closure' {
    Context 'original' {
        $closure = Outer
        It 'runs normally' {
            $closure | Should Not Throw 
        }
    }
    Context 'mock' {
        $Inner = Get-Command -CommandType Function -Name Inner
        Mock Inner { & $Inner @args }
        $closure = Outer
        It 'runs normally' {
            $closure | Should Not Throw 
        }
    }
}

fail with CommandNotFoundException.
The presence of .GetNewClosure() sets the difference between failure and success.
Direct calls from the console, in contrast, don't raise exceptions.

@dlwyatt
Copy link
Member

dlwyatt commented Apr 25, 2016

That's not a Pester problem. Can reproduce the same behavior like this:

Set-StrictMode -Version latest
function Outer {
    {
        Inner
    }.GetNewClosure()
}
function Inner {
    'call Inner'
}

$closure = Outer
& $closure

When you create a new closure, it's essentially creating a script module in memory, behind the scenes, with a snapshot of whatever variables existed at the time the closure was created. The Inner function isn't actually resolved until you execute the closure, though, and at that point, this anonymous script module can't see functions defined in your script (unless that script happens to be dot-sourced into the Global scope.)

Here's how you can make it work in both cases, whether Pester is involved or not:

function Outer {
    $innerFunction = Get-Command Inner -CommandType Function

    {
        & $innerFunction
    }.GetNewClosure()
}

This way, the Inner command resolution happens in the Outer function, not in the closure. Since you assigned the result to a variable, that variable is captured when the closure is created, and everything works fine.

@dlwyatt dlwyatt closed this as completed Apr 25, 2016
@lmmarsano
Copy link
Author

Thanks: that explains a lot.
It unsettles me that the closure call would still not resolve the Inner function when they're in the same script.
Not a Pester problem, though.

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

No branches or pull requests

3 participants