diff --git a/src/functions/Mock.ps1 b/src/functions/Mock.ps1 index 91257db0d..515a04586 100644 --- a/src/functions/Mock.ps1 +++ b/src/functions/Mock.ps1 @@ -475,7 +475,7 @@ function Should-InvokeInternal { if ($matchingCalls.Count -eq $Times -and ($Exactly -or !$PSBoundParameters.ContainsKey('Times'))) { return [Pester.ShouldResult] @{ Succeeded = $false - FailureMessage = "Expected ${commandName}${moduleMessage} not to be called exactly $Times times,$(Format-Because $Because) but it was" + FailureMessage = "Expected ${commandName}${moduleMessage} not to be called exactly $Times times,$(Format-Because $Because) but it was`n$(Format-MockCallHistoryMessage $callHistory $matchingCalls $nonMatchingCalls)" ExpectResult = [Pester.ShouldExpectResult]@{ Expected = "${commandName}${moduleMessage} not to be called exactly $Times times" Actual = "${commandName}${moduleMessage} was called $($matchingCalls.count) times" @@ -486,7 +486,7 @@ function Should-InvokeInternal { elseif ($matchingCalls.Count -ge $Times -and !$Exactly) { return [Pester.ShouldResult] @{ Succeeded = $false - FailureMessage = "Expected ${commandName}${moduleMessage} to be called less than $Times times,$(Format-Because $Because) but was called $($matchingCalls.Count) times" + FailureMessage = "Expected ${commandName}${moduleMessage} to be called less than $Times times,$(Format-Because $Because) but was called $($matchingCalls.Count) times`n$(Format-MockCallHistoryMessage $callHistory $matchingCalls $nonMatchingCalls)" ExpectResult = [Pester.ShouldExpectResult]@{ Expected = "${commandName}${moduleMessage} to be called less than $Times times" Actual = "${commandName}${moduleMessage} was called $($matchingCalls.count) times" @@ -499,7 +499,7 @@ function Should-InvokeInternal { if ($matchingCalls.Count -ne $Times -and ($Exactly -or ($Times -eq 0))) { return [Pester.ShouldResult] @{ Succeeded = $false - FailureMessage = "Expected ${commandName}${moduleMessage} to be called $Times times exactly,$(Format-Because $Because) but was called $($matchingCalls.Count) times" + FailureMessage = "Expected ${commandName}${moduleMessage} to be called $Times times exactly,$(Format-Because $Because) but was called $($matchingCalls.Count) times`n$(Format-MockCallHistoryMessage $callHistory $matchingCalls $nonMatchingCalls)" ExpectResult = [Pester.ShouldExpectResult]@{ Expected = "${commandName}${moduleMessage} to be called $Times times exactly" Actual = "${commandName}${moduleMessage} was called $($matchingCalls.count) times" @@ -510,7 +510,7 @@ function Should-InvokeInternal { elseif ($matchingCalls.Count -lt $Times) { return [Pester.ShouldResult] @{ Succeeded = $false - FailureMessage = "Expected ${commandName}${moduleMessage} to be called at least $Times times,$(Format-Because $Because) but was called $($matchingCalls.Count) times" + FailureMessage = "Expected ${commandName}${moduleMessage} to be called at least $Times times,$(Format-Because $Because) but was called $($matchingCalls.Count) times`n$(Format-MockCallHistoryMessage $callHistory $matchingCalls $nonMatchingCalls)" ExpectResult = [Pester.ShouldExpectResult]@{ Expected = "${commandName}${moduleMessage} to be called at least $Times times" Actual = "${commandName}${moduleMessage} was called $($matchingCalls.count) times" @@ -521,7 +521,7 @@ function Should-InvokeInternal { elseif ($filterIsExclusive -and $nonMatchingCalls.Count -gt 0) { return [Pester.ShouldResult] @{ Succeeded = $false - FailureMessage = "Expected ${commandName}${moduleMessage} to only be called with with parameters matching the specified filter,$(Format-Because $Because) but $($nonMatchingCalls.Count) non-matching calls were made" + FailureMessage = "Expected ${commandName}${moduleMessage} to only be called with with parameters matching the specified filter,$(Format-Because $Because) but $($nonMatchingCalls.Count) non-matching calls were made`n$(Format-MockCallHistoryMessage $callHistory $matchingCalls $nonMatchingCalls)" ExpectResult = [Pester.ShouldExpectResult]@{ Expected = "${commandName}${moduleMessage} to only be called with with parameters matching the specified filter" Actual = "${commandName}${moduleMessage} was called $($nonMatchingCalls.Count) times with non-matching parameters" @@ -1935,3 +1935,31 @@ function Repair-EnumParameters { $sb.ToString() } + +function Format-MockCallHistoryMessage ($callHistory, $matchingCalls, $nonMatchingCalls) { + if ($null -eq $callHistory -or $callHistory.Count -eq 0) { + return "Performed invocations:`n " + } + + $result = "Performed invocations:" + foreach ($historyEntry in $callHistory) { + $params = $historyEntry.BoundParams + if ($null -ne $params -and $params.Count -gt 0) { + $paramText = ($params.GetEnumerator() | ForEach-Object { "-$($_.Key) $(Format-Nicely2 $_.Value)" }) -join " " + } + else { + $paramText = "" + } + + $marker = if ($historyEntry -in $matchingCalls) { "[*]" } else { "[ ]" } + $cmd = $historyEntry.Behavior.CommandName + if ($paramText) { + $result += "`n $marker $cmd $paramText" + } + else { + $result += "`n $marker $cmd" + } + } + + $result +} diff --git a/tst/functions/Mock.Tests.ps1 b/tst/functions/Mock.Tests.ps1 index 2d2c0a4c0..bad2cb968 100644 --- a/tst/functions/Mock.Tests.ps1 +++ b/tst/functions/Mock.Tests.ps1 @@ -1,4 +1,4 @@ -Set-StrictMode -Version Latest +Set-StrictMode -Version Latest BeforeAll { $PSDefaultParameterValues = @{ 'Should:ErrorAction' = 'Stop' } function FunctionUnderTest { @@ -743,7 +743,7 @@ Describe "When Calling Should -Invoke 0 without exactly" { } It "Should throw if mock was called" { - $result.Exception.Message | Should -Be 'Expected FunctionUnderTest to be called 0 times exactly, but was called 1 times' + $result.Exception.Message | Should -BeLike 'Expected FunctionUnderTest to be called 0 times exactly, but was called 1 times*' } It "Should not throw if mock was not called" { @@ -757,7 +757,7 @@ Describe "When Calling Should -Invoke 0 without exactly" { Catch { $failure = $_ } - $failure.Exception.Message | Should -Be 'Expected FunctionUnderTest to be called 0 times exactly, because of reasons, but was called 1 times' + $failure.Exception.Message | Should -BeLike 'Expected FunctionUnderTest to be called 0 times exactly, because of reasons, but was called 1 times*' } } @@ -775,7 +775,7 @@ Describe "When Calling Should -Not -Invoke without exactly" { } It "Should throw if mock was called once" { - $result.Exception.Message | Should -Be "Expected FunctionUnderTest not to be called exactly 1 times, but it was" + $result.Exception.Message | Should -BeLike "Expected FunctionUnderTest not to be called exactly 1 times, but it was*" } It "Should not throw if mock was not called" { @@ -817,7 +817,7 @@ Describe "When Calling Should -Not -Invoke [Times] without exactly" { $result = $_ } - $result.Exception.Message | Should -Be "Expected FunctionUnderTest to be called less than $Times times, but was called $MockCalls times" + $result.Exception.Message | Should -BeLike "Expected FunctionUnderTest to be called less than $Times times, but was called $MockCalls times*" } It 'Should include reason when -Because is used' { @@ -830,7 +830,7 @@ Describe "When Calling Should -Not -Invoke [Times] without exactly" { Catch { $failure = $_ } - $failure.Exception.Message | Should -Be 'Expected FunctionUnderTest to be called less than 1 times, because of reasons, but was called 2 times' + $failure.Exception.Message | Should -BeLike 'Expected FunctionUnderTest to be called less than 1 times, because of reasons, but was called 2 times*' } } @@ -849,7 +849,7 @@ Describe "When Calling Should -Invoke with exactly" { } It "Should throw if mock was not called the number of times specified" { - $result.Exception.Message | Should -Be "Expected FunctionUnderTest to be called 3 times exactly, but was called 2 times" + $result.Exception.Message | Should -BeLike "Expected FunctionUnderTest to be called 3 times exactly, but was called 2 times*" } It "Should not throw if mock was called the number of times specified" { @@ -871,7 +871,7 @@ Describe "When Calling Should -Not -Invoke with exactly" { } It "Should throw if mock was called" { - $result.Exception.Message | Should -Be "Expected FunctionUnderTest not to be called exactly 1 times, but it was" + $result.Exception.Message | Should -BeLike "Expected FunctionUnderTest not to be called exactly 1 times, but it was*" } It "Should not throw if mock was not called" { @@ -885,7 +885,7 @@ Describe "When Calling Should -Not -Invoke with exactly" { Catch { $failure = $_ } - $failure.Exception.Message | Should -Be 'Expected FunctionUnderTest not to be called exactly 1 times, because of reasons, but it was' + $failure.Exception.Message | Should -BeLike 'Expected FunctionUnderTest not to be called exactly 1 times, because of reasons, but it was*' } } @@ -924,7 +924,7 @@ Describe "When Calling Should -Not -Invoke [Times] with exactly" { $result = $_ } - $result.Exception.Message | Should -Be "Expected FunctionUnderTest not to be called exactly $Times times, but it was" + $result.Exception.Message | Should -BeLike "Expected FunctionUnderTest not to be called exactly $Times times, but it was*" } } @@ -938,7 +938,7 @@ Describe "When Calling Should -Invoke without exactly" { It "Should throw if mock was not called at least the number of times specified" { $scriptBlock = { Should -Invoke FunctionUnderTest 4 -Scope Describe } - $scriptBlock | Should -Throw "Expected FunctionUnderTest to be called at least 4 times, but was called 3 times" + $scriptBlock | Should -Throw "Expected FunctionUnderTest to be called at least 4 times, but was called 3 times*" } It "Should not throw if mock was called at least the number of times specified" { @@ -961,7 +961,7 @@ Describe "When Calling Should -Invoke without exactly" { Catch { $failure = $_ } - $failure.Exception.Message | Should -Be 'Expected FunctionUnderTest to be called at least 4 times, because of reasons, but was called 3 times' + $failure.Exception.Message | Should -BeLike 'Expected FunctionUnderTest to be called at least 4 times, because of reasons, but was called 3 times*' } It 'Should include reason when -Because is used with -ExclusiveFilter' { @@ -971,7 +971,7 @@ Describe "When Calling Should -Invoke without exactly" { Catch { $failure = $_ } - $failure.Exception.Message | Should -Be 'Expected FunctionUnderTest to only be called with with parameters matching the specified filter, because of reasons, but 1 non-matching calls were made' + $failure.Exception.Message | Should -BeLike 'Expected FunctionUnderTest to only be called with with parameters matching the specified filter, because of reasons, but 1 non-matching calls were made*' } }