Skip to content

Commit

Permalink
Fix session state in Should -Invoke to use the correct session state (#…
Browse files Browse the repository at this point in the history
…1954)

* Fix session state in Should -Invoke to use the correct session state

* Add tests
  • Loading branch information
nohwnd authored May 21, 2021
1 parent 7e6e83b commit 91f9795
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/functions/Mock.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,10 @@ function Should-InvokeInternal {
BoundParameters = $historyEntry.BoundParams
ArgumentList = $historyEntry.Args
Metadata = $ContextInfo.Hook.Metadata
SessionState = $ContextInfo.Hook.CallerSessionState
# do not use the callser session state from the hook, the parameter filter
# on Should -Invoke can come from a different session state if inModuleScope is used to
# wrap it. Use the caller session state to which the scriptblock is bound
SessionState = $SessionState
}

# if ($null -ne $ContextInfo.Hook.Metadata -and $null -ne $params.ScriptBlock) {
Expand Down
74 changes: 73 additions & 1 deletion tst/Pester.Mock.RSpec.ts.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1129,7 +1129,79 @@ i -PassThru:$PassThru {
Run = @{ ScriptBlock = $sb; PassThru = $true }
})

$t = $r.Containers[0].Blocks[0].Tests[0]
$t = $r.Containers[0]
$t.Result | Verify-Equal "Passed"
}
}

b "Should invoke parameter filter works when exexuted in a different module than the mock" {
t "Should invoke can be invoked in module scope and it still uses the correct session state" {
# https://github.com/pester/Pester/issues/1813

$sb = {
BeforeAll {
$script:moduleName = 'MyModule'

Remove-Module -Name 'MyModule' -Force -ErrorAction 'SilentlyContinue'

New-Module -Name 'MyModule' -ScriptBlock {
function Get-MyAlert {
write-warning "real function called!"
}

function New-MyAlert {
Get-MyAlert

$null = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Agent.Alert
}
} | Import-Module

$PSDefaultParameterValues = @{
'InModuleScope:ModuleName' = $script:moduleName
}
}

Describe 'InModuleScope' {
BeforeAll {
Mock -CommandName Get-MyAlert -ModuleName $script:moduleName
Mock -CommandName New-Object -ModuleName $script:moduleName -MockWith {
return 'anything'
} -ParameterFilter {
$TypeName -eq 'Microsoft.SqlServer.Management.Smo.Agent.Alert'
}
}

It 'Should call the mock' {
InModuleScope -ScriptBlock {
{ New-MyAlert } | Should -Not -Throw
}

Should -Invoke -CommandName Get-MyAlert -ModuleName $script:moduleName -Exactly -Times 1 -Scope It

Should -Invoke -CommandName New-Object -ModuleName $script:moduleName -ParameterFilter {
$TypeName -eq 'Microsoft.SqlServer.Management.Smo.Agent.Alert'
} -Exactly -Times 1 -Scope It
}

It 'Should call the mock' {
InModuleScope -ScriptBlock {
{ New-MyAlert } | Should -Not -Throw

Should -Invoke -CommandName Get-MyAlert -Exactly -Times 1 -Scope It

Should -Invoke -CommandName New-Object -ParameterFilter {
$TypeName -eq 'Microsoft.SqlServer.Management.Smo.Agent.Alert'
} -Exactly -Times 1 -Scope It
}
}
}
}

$r = Invoke-Pester -Configuration ([PesterConfiguration]@{
Run = @{ ScriptBlock = $sb; PassThru = $true }
})

$t = $r.Containers[0]
$t.Result | Verify-Equal "Passed"
}
}
Expand Down

0 comments on commit 91f9795

Please sign in to comment.