You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Print mock history
* Polish mock call history and update tests to use -BeLike
Refine Format-MockCallHistoryMessage: use [*]/[ ] markers for matching
vs non-matching calls, format parameter values with Format-Nicely2,
handle zero-call case with <none>.
Update Should-Invoke test assertions from exact -Be to -BeLike with
wildcard suffix so they tolerate the appended call history text.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
* Use foreach keyword instead of ForEach-Object in Format-MockCallHistoryMessage
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
* Add dedicated tests for mock call history output format
Test the full failure message including history section for:
- Filter matches 0 out of 3 calls (all [ ])
- Filter matches 1 out of 3 but expected 2 (mix of [*] and [ ])
- All 3 match but expected 4 (all [*])
- Zero calls (shows <none>)
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
* Add mock behavior location to call history output
Show file:line of the Mock -MockWith scriptblock in the invocation
history so users can identify which behavior handled each call.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
* Tighten mock history test assertions
Spell out markers and command names, only wildcard path prefix and
line numbers to avoid brittleness.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
* Fix merge conflict markers in test file
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
---------
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copy file name to clipboardExpand all lines: src/functions/Mock.ps1
+43-5Lines changed: 43 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -470,7 +470,7 @@ function Should-InvokeInternal {
470
470
if ($matchingCalls.Count-eq$Times-and ($Exactly-or!$PSBoundParameters.ContainsKey('Times'))) {
471
471
return [Pester.ShouldResult] @{
472
472
Succeeded=$false
473
-
FailureMessage="Expected ${commandName}${moduleMessage} not to be called exactly $Times times,$(Format-Because$Because) but it was"
473
+
FailureMessage="Expected ${commandName}${moduleMessage} not to be called exactly $Times times,$(Format-Because$Because) but it was`n$(Format-MockCallHistoryMessage$callHistory$matchingCalls$nonMatchingCalls)"
474
474
ExpectResult= [Pester.ShouldExpectResult]@{
475
475
Expected="${commandName}${moduleMessage} not to be called exactly $Times times"
476
476
Actual="${commandName}${moduleMessage} was called $($matchingCalls.count) times"
@@ -481,7 +481,7 @@ function Should-InvokeInternal {
FailureMessage="Expected ${commandName}${moduleMessage} to be called less than $Times times,$(Format-Because$Because) but was called $($matchingCalls.Count) times"
484
+
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)"
485
485
ExpectResult= [Pester.ShouldExpectResult]@{
486
486
Expected="${commandName}${moduleMessage} to be called less than $Times times"
487
487
Actual="${commandName}${moduleMessage} was called $($matchingCalls.count) times"
@@ -494,7 +494,7 @@ function Should-InvokeInternal {
494
494
if ($matchingCalls.Count-ne$Times-and ($Exactly-or ($Times-eq0))) {
495
495
return [Pester.ShouldResult] @{
496
496
Succeeded=$false
497
-
FailureMessage="Expected ${commandName}${moduleMessage} to be called $Times times exactly,$(Format-Because$Because) but was called $($matchingCalls.Count) times"
497
+
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)"
498
498
ExpectResult= [Pester.ShouldExpectResult]@{
499
499
Expected="${commandName}${moduleMessage} to be called $Times times exactly"
500
500
Actual="${commandName}${moduleMessage} was called $($matchingCalls.count) times"
@@ -505,7 +505,7 @@ function Should-InvokeInternal {
505
505
elseif ($matchingCalls.Count-lt$Times) {
506
506
return [Pester.ShouldResult] @{
507
507
Succeeded=$false
508
-
FailureMessage="Expected ${commandName}${moduleMessage} to be called at least $Times times,$(Format-Because$Because) but was called $($matchingCalls.Count) times"
508
+
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)"
509
509
ExpectResult= [Pester.ShouldExpectResult]@{
510
510
Expected="${commandName}${moduleMessage} to be called at least $Times times"
511
511
Actual="${commandName}${moduleMessage} was called $($matchingCalls.count) times"
@@ -516,7 +516,7 @@ function Should-InvokeInternal {
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"
519
+
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)"
520
520
ExpectResult= [Pester.ShouldExpectResult]@{
521
521
Expected="${commandName}${moduleMessage} to only be called with with parameters matching the specified filter"
522
522
Actual="${commandName}${moduleMessage} was called $($nonMatchingCalls.Count) times with non-matching parameters"
@@ -1879,3 +1879,41 @@ function Repair-EnumParameters {
@@ -743,7 +743,7 @@ Describe "When Calling Should -Invoke 0 without exactly" {
743
743
}
744
744
745
745
It "Should throw if mock was called" {
746
-
$result.Exception.Message| Should -Be'Expected FunctionUnderTest to be called 0 times exactly, but was called 1 times'
746
+
$result.Exception.Message| Should -BeLike'Expected FunctionUnderTest to be called 0 times exactly, but was called 1 times*'
747
747
}
748
748
749
749
It "Should not throw if mock was not called" {
@@ -757,7 +757,7 @@ Describe "When Calling Should -Invoke 0 without exactly" {
757
757
Catch {
758
758
$failure=$_
759
759
}
760
-
$failure.Exception.Message| Should -Be'Expected FunctionUnderTest to be called 0 times exactly, because of reasons, but was called 1 times'
760
+
$failure.Exception.Message| Should -BeLike'Expected FunctionUnderTest to be called 0 times exactly, because of reasons, but was called 1 times*'
761
761
}
762
762
}
763
763
@@ -775,7 +775,7 @@ Describe "When Calling Should -Not -Invoke without exactly" {
775
775
}
776
776
777
777
It "Should throw if mock was called once" {
778
-
$result.Exception.Message| Should -Be"Expected FunctionUnderTest not to be called exactly 1 times, but it was"
778
+
$result.Exception.Message| Should -BeLike"Expected FunctionUnderTest not to be called exactly 1 times, but it was*"
779
779
}
780
780
781
781
It "Should not throw if mock was not called" {
@@ -817,7 +817,7 @@ Describe "When Calling Should -Not -Invoke [Times] without exactly" {
817
817
$result=$_
818
818
}
819
819
820
-
$result.Exception.Message| Should -Be"Expected FunctionUnderTest to be called less than $Times times, but was called $MockCalls times"
820
+
$result.Exception.Message| Should -BeLike"Expected FunctionUnderTest to be called less than $Times times, but was called $MockCalls times*"
821
821
}
822
822
823
823
It 'Should include reason when -Because is used' {
@@ -830,7 +830,7 @@ Describe "When Calling Should -Not -Invoke [Times] without exactly" {
830
830
Catch {
831
831
$failure=$_
832
832
}
833
-
$failure.Exception.Message| Should -Be'Expected FunctionUnderTest to be called less than 1 times, because of reasons, but was called 2 times'
833
+
$failure.Exception.Message| Should -BeLike'Expected FunctionUnderTest to be called less than 1 times, because of reasons, but was called 2 times*'
834
834
}
835
835
}
836
836
@@ -849,7 +849,7 @@ Describe "When Calling Should -Invoke with exactly" {
849
849
}
850
850
851
851
It "Should throw if mock was not called the number of times specified" {
852
-
$result.Exception.Message| Should -Be"Expected FunctionUnderTest to be called 3 times exactly, but was called 2 times"
852
+
$result.Exception.Message| Should -BeLike"Expected FunctionUnderTest to be called 3 times exactly, but was called 2 times*"
853
853
}
854
854
855
855
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" {
871
871
}
872
872
873
873
It "Should throw if mock was called" {
874
-
$result.Exception.Message| Should -Be"Expected FunctionUnderTest not to be called exactly 1 times, but it was"
874
+
$result.Exception.Message| Should -BeLike"Expected FunctionUnderTest not to be called exactly 1 times, but it was*"
875
875
}
876
876
877
877
It "Should not throw if mock was not called" {
@@ -885,7 +885,7 @@ Describe "When Calling Should -Not -Invoke with exactly" {
885
885
Catch {
886
886
$failure=$_
887
887
}
888
-
$failure.Exception.Message| Should -Be'Expected FunctionUnderTest not to be called exactly 1 times, because of reasons, but it was'
888
+
$failure.Exception.Message| Should -BeLike'Expected FunctionUnderTest not to be called exactly 1 times, because of reasons, but it was*'
889
889
}
890
890
}
891
891
@@ -924,7 +924,7 @@ Describe "When Calling Should -Not -Invoke [Times] with exactly" {
924
924
$result=$_
925
925
}
926
926
927
-
$result.Exception.Message| Should -Be"Expected FunctionUnderTest not to be called exactly $Times times, but it was"
927
+
$result.Exception.Message| Should -BeLike"Expected FunctionUnderTest not to be called exactly $Times times, but it was*"
928
928
}
929
929
}
930
930
@@ -938,7 +938,7 @@ Describe "When Calling Should -Invoke without exactly" {
938
938
939
939
It "Should throw if mock was not called at least the number of times specified" {
940
940
$scriptBlock= { Should -Invoke FunctionUnderTest 4-Scope Describe }
941
-
$scriptBlock| Should -Throw "Expected FunctionUnderTest to be called at least 4 times, but was called 3 times"
941
+
$scriptBlock| Should -Throw "Expected FunctionUnderTest to be called at least 4 times, but was called 3 times*"
942
942
}
943
943
944
944
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" {
961
961
Catch {
962
962
$failure=$_
963
963
}
964
-
$failure.Exception.Message| Should -Be'Expected FunctionUnderTest to be called at least 4 times, because of reasons, but was called 3 times'
964
+
$failure.Exception.Message| Should -BeLike'Expected FunctionUnderTest to be called at least 4 times, because of reasons, but was called 3 times*'
965
965
}
966
966
967
967
It 'Should include reason when -Because is used with -ExclusiveFilter' {
@@ -971,7 +971,84 @@ Describe "When Calling Should -Invoke without exactly" {
971
971
Catch {
972
972
$failure=$_
973
973
}
974
-
$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'
974
+
$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*'
975
+
}
976
+
}
977
+
978
+
Describe "Mock call history in Should -Invoke failure messages" {
979
+
It "Shows all calls as non-matching when filter matches 0 out of 3" {
980
+
Mock FunctionUnderTest { }
981
+
FunctionUnderTest "one"
982
+
FunctionUnderTest "two"
983
+
FunctionUnderTest "three"
984
+
985
+
try {
986
+
Should -Invoke FunctionUnderTest -ParameterFilter { $param1-eq'four' }
987
+
}
988
+
catch {
989
+
$failure=$_
990
+
}
991
+
992
+
$failure.Exception.Message| Should -BeLike ("Expected FunctionUnderTest*was called 0 times
993
+
Performed invocations:
994
+
[[] ] FunctionUnderTest -param1 'one' from *Mock.Tests.ps1:*
995
+
[[] ] FunctionUnderTest -param1 'two' from *Mock.Tests.ps1:*
996
+
[[] ] FunctionUnderTest -param1 'three' from *Mock.Tests.ps1:*"-replace"`r`n","`n")
997
+
}
998
+
999
+
It "Shows matching and non-matching calls when filter matches 1 out of 3 but expected 2" {
1000
+
Mock FunctionUnderTest { }
1001
+
FunctionUnderTest "one"
1002
+
FunctionUnderTest "two"
1003
+
FunctionUnderTest "one"
1004
+
1005
+
try {
1006
+
Should -Invoke FunctionUnderTest -Exactly 2-ParameterFilter { $param1-eq'two' }
1007
+
}
1008
+
catch {
1009
+
$failure=$_
1010
+
}
1011
+
1012
+
$failure.Exception.Message| Should -BeLike ("Expected FunctionUnderTest*was called 1 times*
1013
+
Performed invocations:
1014
+
[[] ] FunctionUnderTest -param1 'one' from *Mock.Tests.ps1:*
1015
+
[[]*] FunctionUnderTest -param1 'two' from *Mock.Tests.ps1:*
1016
+
[[] ] FunctionUnderTest -param1 'one' from *Mock.Tests.ps1:*"-replace"`r`n","`n")
1017
+
}
1018
+
1019
+
It "Shows all calls as matching when all 3 match but expected 4" {
1020
+
Mock FunctionUnderTest { }
1021
+
FunctionUnderTest "one"
1022
+
FunctionUnderTest "one"
1023
+
FunctionUnderTest "one"
1024
+
1025
+
try {
1026
+
Should -Invoke FunctionUnderTest -Exactly 4-ParameterFilter { $param1-eq'one' }
1027
+
}
1028
+
catch {
1029
+
$failure=$_
1030
+
}
1031
+
1032
+
$failure.Exception.Message| Should -BeLike ("Expected FunctionUnderTest*was called 3 times*
1033
+
Performed invocations:
1034
+
[[]*] FunctionUnderTest -param1 'one' from *Mock.Tests.ps1:*
1035
+
[[]*] FunctionUnderTest -param1 'one' from *Mock.Tests.ps1:*
1036
+
[[]*] FunctionUnderTest -param1 'one' from *Mock.Tests.ps1:*"-replace"`r`n","`n")
1037
+
}
1038
+
1039
+
It 'Shows empty marker when mock was never called' {
1040
+
Mock FunctionUnderTest { }
1041
+
1042
+
try {
1043
+
Should -Invoke FunctionUnderTest -Exactly 1
1044
+
}
1045
+
catch {
1046
+
$failure=$_
1047
+
}
1048
+
1049
+
$failure.Exception.Message| Should -Be ('Expected FunctionUnderTest to be called 1 times exactly, but was called 0 times
0 commit comments