Skip to content
Discussion options

You must be logged in to vote

$? tells you if the last command succeeded, and a mock is a function, so it stays true as long as the function itself does not fail. This is not specific to mocking, a plain function does the same:

function f { & /bin/sh -c 'exit 128' }
f
$?             # True
$LASTEXITCODE  # 128

$LASTEXITCODE propagates fine, so assert on that.

To get $? false the mock would have to write an error, and that does not really work here. Write-Error in -MockWith does not flip it, and $PSCmdlet is $null inside the mock body so the WriteError from your edit is not available. throw does flip it, but it is terminating so probably not what you want.

Tested with Pester 6.1.0 on PowerShell 7.5.5.

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by nohwnd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants