Pester icon indicating copy to clipboard operation
Pester copied to clipboard

Add how to call original function to Mock help

Open nohwnd opened this issue 2 years ago • 1 comments

Don't know what you do wrong, it works for me, when the function is in module and even if it is not in module:

New-Module a -ScriptBlock {
    function My-Function () { 
        Write-Host "Hello"
    }  
} | Import-Module

Describe "Describe" {
    Context "Context1 PASSES" {
        Mock My-Function { 
            $myFunction = Get-Command My-Function -CommandType Function
            return & $myFunction
        }

        My-Function
    }

    Context "Context2 FAILS" {
        Mock My-Function { 
            $myFunction = Get-Command My-Function -CommandType Function
            return & $myFunction
        }

        My-Function
    }
}

This is latest Pester 4, which version are you using? This probably would not work on very old Pester where aliases are not used for Mocking, but instead of that functions are used.

Originally posted by @nohwnd in https://github.com/pester/Pester/issues/578#issuecomment-688762755

nohwnd avatar Apr 22 '22 20:04 nohwnd

Indirectly covered in pester/docs#184

fflaten avatar May 02 '22 22:05 fflaten