diff --git a/src/functions/assert/Mock/Should-Invoke.ps1 b/src/functions/assert/Mock/Should-Invoke.ps1 index 8dc3f388c..3f729cbf1 100644 --- a/src/functions/assert/Mock/Should-Invoke.ps1 +++ b/src/functions/assert/Mock/Should-Invoke.ps1 @@ -183,7 +183,8 @@ if ($PSBoundParameters.ContainsKey('Verifiable')) { $PSBoundParameters.Remove('Verifiable') - Should-InvokeVerifiable @PSBoundParameters + $testResult = Should-InvokeVerifiable @PSBoundParameters + Test-AssertionResult $testResult return } @@ -192,5 +193,7 @@ $PSBoundParameters["ActualValue"] = $null $PSBoundParameters["Negate"] = $false $PSBoundParameters["CallerSessionState"] = $PSCmdlet.SessionState - Should-InvokeAssertion @PSBoundParameters + $testResult = Should-InvokeAssertion @PSBoundParameters + + Test-AssertionResult $testResult } diff --git a/src/functions/assert/Mock/Should-NotInvoke.ps1 b/src/functions/assert/Mock/Should-NotInvoke.ps1 index 8d056dbc5..dc751190c 100644 --- a/src/functions/assert/Mock/Should-NotInvoke.ps1 +++ b/src/functions/assert/Mock/Should-NotInvoke.ps1 @@ -172,7 +172,8 @@ if ($PSBoundParameters.ContainsKey('Verifiable')) { $PSBoundParameters.Remove('Verifiable') - Should-InvokeVerifiable @PSBoundParameters + $testResult = Should-InvokeVerifiable @PSBoundParameters + Test-AssertionResult $testResult return } @@ -180,5 +181,7 @@ # possible to register as Should operator. $PSBoundParameters["ActualValue"] = $null $PSBoundParameters["CallerSessionState"] = $PSCmdlet.SessionState - Should-InvokeAssertion @PSBoundParameters + $testResult = Should-InvokeAssertion @PSBoundParameters + + Test-AssertionResult $testResult } diff --git a/src/functions/assertions/Should.ps1 b/src/functions/assertions/Should.ps1 index fc0130d4d..785fc92b5 100644 --- a/src/functions/assertions/Should.ps1 +++ b/src/functions/assertions/Should.ps1 @@ -238,8 +238,16 @@ function Invoke-Assertion { $testResult = & $AssertionEntry.Test -ActualValue $ValueToTest -Negate:$Negate -CallerSessionState $CallerSessionState @BoundParameters - if (-not $testResult.Succeeded) { - $errorRecord = [Pester.Factory]::CreateShouldErrorRecord($testResult.FailureMessage, $file, $lineNumber, $lineText, $shouldThrow, $testResult) + Test-AssertionResult $testResult +} + +function Test-AssertionResult { + param ( + $TestResult + ) + + if (-not $TestResult.Succeeded) { + $errorRecord = [Pester.Factory]::CreateShouldErrorRecord($TestResult.FailureMessage, $file, $lineNumber, $lineText, $shouldThrow, $TestResult) if ($null -eq $AddErrorCallback -or $ShouldThrow) { # throw this error to fail the test immediately @@ -261,11 +269,12 @@ function Invoke-Assertion { } else { #extract data to return if there are any on the object - $data = $testResult.psObject.Properties.Item('Data') + $data = $TestResult.psObject.Properties.Item('Data') if ($data) { $data.Value } } + } function Format-Because ([string] $Because) { diff --git a/tst/functions/Output.Tests.ps1 b/tst/functions/Output.Tests.ps1 index 3b54bedc3..ef72ac2e6 100644 --- a/tst/functions/Output.Tests.ps1 +++ b/tst/functions/Output.Tests.ps1 @@ -186,8 +186,7 @@ InModuleScope -ModuleName Pester -ScriptBlock { $r.Message.Count | Should -be 6 $r.Trace[0] | Should -match "'One' | Should -be 'Two'" - $r.Trace[1] | Should -be "at , ${PSCommandPath}:172" - $r.Trace.Count | Should -be 2 + $r.Trace.Count | Should -be 1 } # TODO: should fails with a very weird error, probably has something to do with dynamic params... # Context 'Should fails in file' { @@ -265,7 +264,7 @@ InModuleScope -ModuleName Pester -ScriptBlock { $r.Trace[0] | Should -be "at f1, ${testPath}:2" $r.Trace[1] | Should -be "at f2, ${testPath}:5" $r.Trace[2] | Should -be "at , ${testPath}:7" - $r.Trace[3] | Should -be "at , ${PSCommandPath}:248" + $r.Trace[3] | Should -be "at , ${PSCommandPath}:247" $r.Trace.Count | Should -be 4 } } @@ -276,7 +275,7 @@ InModuleScope -ModuleName Pester -ScriptBlock { $r.Trace[0] | Should -be "at f1, ${testPath}:2" $r.Trace[1] | Should -be "at f2, ${testPath}:5" $r.Trace[2] | Should -be "at , ${testPath}:7" - $r.Trace[3] | Should -be "at , ${PSCommandPath}:248" + $r.Trace[3] | Should -be "at , ${PSCommandPath}:247" $r.Trace.Count | Should -be 4 } } @@ -337,7 +336,7 @@ InModuleScope -ModuleName Pester -ScriptBlock { It 'produces correct trace line.' { if ($hasStackTrace) { $r.Trace[0] | Should -be "at , $testPath`:10" - $r.Trace[1] | Should -be "at , $PSCommandPath`:314" + $r.Trace[1] | Should -be "at , $PSCommandPath`:313" $r.Trace.Count | Should -be 2 } } @@ -346,7 +345,7 @@ InModuleScope -ModuleName Pester -ScriptBlock { It 'produces correct trace line.' { if ($hasStackTrace) { $r.Trace[0] | Should -be "at , $testPath`:10" - $r.Trace[1] | Should -be "at , $PSCommandPath`:314" + $r.Trace[1] | Should -be "at , $PSCommandPath`:313" $r.Trace.Count | Should -be 2 } } @@ -435,7 +434,7 @@ InModuleScope -ModuleName Pester -ScriptBlock { $errorMessage = Format-ErrorMessage -Err $errorRecord -StackTraceVerbosity $_ $messages = $errorMessage -split [Environment]::NewLine $messages[0] | Should -BeExactly "System.DivideByZeroException: Attempted to divide by zero." - $messages[1] | Should -BeExactly "at , ${PSCommandPath}: line 389" + $messages[1] | Should -BeExactly "at , ${PSCommandPath}: line 388" $messages.Count | Should -BeGreaterThan 1 } diff --git a/tst/functions/assert/Mock/Should-NotInvoke.Tests.ps1 b/tst/functions/assert/Mock/Should-NotInvoke.Tests.ps1 index aa9b194c5..9b00131f3 100644 --- a/tst/functions/assert/Mock/Should-NotInvoke.Tests.ps1 +++ b/tst/functions/assert/Mock/Should-NotInvoke.Tests.ps1 @@ -1,11 +1,11 @@ Set-StrictMode -Version Latest -Describe "Should-Invoke" { +Describe "Should-NotInvoke" { It "Passes when Mock was not invoked" { function f () { } Mock f - Should-Invoke f -Times 1 -Exactly + Should-NotInvoke f -Times 1 -Exactly } It "Fails when Mock was invoked" { @@ -14,7 +14,7 @@ Describe "Should-Invoke" { f - { Should-Invoke f -Times 1 -Exactly } | Verify-Throw + { Should-NotInvoke f -Times 1 -Exactly } | Verify-Throw } }