Skip to content

Commit

Permalink
actually throw exception when failing
Browse files Browse the repository at this point in the history
  • Loading branch information
nohwnd committed Dec 17, 2024
1 parent 005d756 commit 90a2494
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 17 deletions.
7 changes: 5 additions & 2 deletions src/functions/assert/Mock/Should-Invoke.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@

if ($PSBoundParameters.ContainsKey('Verifiable')) {
$PSBoundParameters.Remove('Verifiable')
Should-InvokeVerifiable @PSBoundParameters
$testResult = Should-InvokeVerifiable @PSBoundParameters
Test-AssertionResult $testResult
return
}

Expand All @@ -192,5 +193,7 @@
$PSBoundParameters["ActualValue"] = $null
$PSBoundParameters["Negate"] = $false
$PSBoundParameters["CallerSessionState"] = $PSCmdlet.SessionState
Should-InvokeAssertion @PSBoundParameters
$testResult = Should-InvokeAssertion @PSBoundParameters

Test-AssertionResult $testResult
}
7 changes: 5 additions & 2 deletions src/functions/assert/Mock/Should-NotInvoke.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,16 @@

if ($PSBoundParameters.ContainsKey('Verifiable')) {
$PSBoundParameters.Remove('Verifiable')
Should-InvokeVerifiable @PSBoundParameters
$testResult = Should-InvokeVerifiable @PSBoundParameters
Test-AssertionResult $testResult
return
}

# Maps the parameters so we can internally use functions that is
# possible to register as Should operator.
$PSBoundParameters["ActualValue"] = $null
$PSBoundParameters["CallerSessionState"] = $PSCmdlet.SessionState
Should-InvokeAssertion @PSBoundParameters
$testResult = Should-InvokeAssertion @PSBoundParameters

Test-AssertionResult $testResult
}
15 changes: 12 additions & 3 deletions src/functions/assertions/Should.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down
13 changes: 6 additions & 7 deletions tst/functions/Output.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 <ScriptBlock>, ${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' {
Expand Down Expand Up @@ -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 <ScriptBlock>, ${testPath}:7"
$r.Trace[3] | Should -be "at <ScriptBlock>, ${PSCommandPath}:248"
$r.Trace[3] | Should -be "at <ScriptBlock>, ${PSCommandPath}:247"
$r.Trace.Count | Should -be 4
}
}
Expand All @@ -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 <ScriptBlock>, ${testPath}:7"
$r.Trace[3] | Should -be "at <ScriptBlock>, ${PSCommandPath}:248"
$r.Trace[3] | Should -be "at <ScriptBlock>, ${PSCommandPath}:247"
$r.Trace.Count | Should -be 4
}
}
Expand Down Expand Up @@ -337,7 +336,7 @@ InModuleScope -ModuleName Pester -ScriptBlock {
It 'produces correct trace line.' {
if ($hasStackTrace) {
$r.Trace[0] | Should -be "at <ScriptBlock>, $testPath`:10"
$r.Trace[1] | Should -be "at <ScriptBlock>, $PSCommandPath`:314"
$r.Trace[1] | Should -be "at <ScriptBlock>, $PSCommandPath`:313"
$r.Trace.Count | Should -be 2
}
}
Expand All @@ -346,7 +345,7 @@ InModuleScope -ModuleName Pester -ScriptBlock {
It 'produces correct trace line.' {
if ($hasStackTrace) {
$r.Trace[0] | Should -be "at <ScriptBlock>, $testPath`:10"
$r.Trace[1] | Should -be "at <ScriptBlock>, $PSCommandPath`:314"
$r.Trace[1] | Should -be "at <ScriptBlock>, $PSCommandPath`:313"
$r.Trace.Count | Should -be 2
}
}
Expand Down Expand Up @@ -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 <ScriptBlock>, ${PSCommandPath}: line 389"
$messages[1] | Should -BeExactly "at <ScriptBlock>, ${PSCommandPath}: line 388"
$messages.Count | Should -BeGreaterThan 1
}

Expand Down
6 changes: 3 additions & 3 deletions tst/functions/assert/Mock/Should-NotInvoke.Tests.ps1
Original file line number Diff line number Diff line change
@@ -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" {
Expand All @@ -14,7 +14,7 @@ Describe "Should-Invoke" {

f

{ Should-Invoke f -Times 1 -Exactly } | Verify-Throw
{ Should-NotInvoke f -Times 1 -Exactly } | Verify-Throw
}
}

Expand Down

0 comments on commit 90a2494

Please sign in to comment.