Unable to Mock when running Pester in its own process/scope #2061
-
As I am continuing to struggle with using Pester to test Pester commands, I have come upon a new problem. As described in this issue, I am now executing the Pester commands to be verified within its own scope, which works much better. However, at a certain stage I need to be able to mock some commands that the functionality to be tested is using: #CommonFunctions.psm1
Function Get-SomeItem {
# Retrieves data from net
}
Export-ModuleMember -Function Get-SomeItem #MockedFunctions.psm1
Function Get-MockedItem {
# Mocks data retrieval from net
}
Export-ModuleMember -Function Get-MockedItem The actual script: Describe "VerifyTests" {
BeforeAll {
$target = 'ScriptToVerify.ps1'
$pesterModulePath = (Get-Module Pester).Path -replace '\.psm1','.psd1'
$CommonFunctionsModulePath = "$PSScriptRoot\CommonFunctions.psm1"
$MockedFunctionsModulePath = "$PSScriptRoot\MockedFunctions.psm1"
}
# Correct configurations
It "ShouldPass" {
# Check items with correct setup
$correctRun = Start-Job {
Import-Module $using:pesterModulePath
Import-Module $using:CommonFunctionsModulePath
Import-Module $using:MockedFunctionsModulePath
Mock Get-SomeItem -ModuleName CommonModule -ParameterFilter { $Item = 1 } { Get-MockedItem 1 }
$container = New-PesterContainer -Path "$using:target" -Data @{Item=1}
Invoke-Pester -Container $container -PassThru -Output None
} | Receive-Job -Wait
$correctRun.TotalCount | Should -Be 1
$correctRun.FailedCount | Should -Be 0
}
}
} But when trying to execute this, the only result I get is |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
You can't call |
Beta Was this translation helpful? Give feedback.
You can't call
Mock
beforeInvoke-Pester
in your job (which is a new process). Mocks need to be created during a Pester-run in the container where it's required. In other words, inside theScriptToVerify.ps1
testscript.