mardi 21 juin 2016

Pester not mocking function that is dot-sourced

I'm using Pester to test a PowerShell script that dot-sources another script. When I try to mock the function that is dot-sourced, Pester refuses to use the mocked version. I'm having the same problem when I try to source the function by using adding it to a .psm1 file and using Import-Module instead of dot-sourcing.

Here's an example that replicates the problem I'm having. All 3 files are in the same folder.

Foo.ps1

Function Invoke-Foo{
    'Cantelope'
}

Bar.ps1

function Invoke-Bar {
    . .\foo.ps1
    Invoke-foo
}

Bar.tests.ps1

$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.', '.'
. "$here\$sut"
. .\Foo.ps1

Describe "Bar" {
    It "Mocks Foo" {
        Mock Invoke-Foo {'Banana'}
        Invoke-Bar | should be 'Banana'
    }
}

After mocking Invoke-Foo, the result should be 'Banana', but the result is:

Describing Bar
 [-] Mocks Foo 36ms
   Expected string length 6 but was 9. Strings differ at index 0.
   Expected: {Banana}
   But was:  {Cantelope}
   -----------^
   9:         Invoke-Bar | should be 'Banana'
   at <ScriptBlock>, C:\Users\geinosky\Desktop\PingTest\Bar.tests.ps1: line      9

How can I get Pester to correctly work with the dot-sourced function?

Aucun commentaire:

Enregistrer un commentaire