I want to create a mock with Pester which is supposed to be called in stead of a script. Simplified example below.
somecmd.ps1
# This is the script I want to not be called in my test.
Write-Host "Output from somecmd.ps1"
throw "Error, should have been mocked"
somemodule.psm1
# This is the module I am testing.
function ModuleFunction{
.\somecmd
}
function somecmd {
Write-Host "Output from somecmd in somemodule"
throw "Error, should never be called"
}
somemodule.Tests.ps1
# This is my Pester test.
BeforeAll {
Import-Module .\somemodule.psm1 -Force
}
Describe 'SomeTest' {
It 'Should mock' {
# How must I declare the mock below so that somecmd.ps1 is not invoked?
Mock somecmd { Write-Host "Output from mock" } -ModuleName somemodule
ModuleFunction
}
}
As stated in the comment above, my problem is I cannot figure out how to declare the mock, so that the test will use that in stead of actually invoking somecmd.ps1.
I tried looking into this issue, but following the suggestion there did not solve my problem.
Unfortunately, in my real scenario, it is not an option for me to re-write the module to better support testing.
I am running:
- PowerShell: 5.1.19041.1682
- Pester: 5.3.3
Does anyone have an idea?