From 005d75658f1a2621eccbc58ca22f6dcf549a9a32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Jare=C5=A1?= Date: Mon, 16 Dec 2024 16:27:15 +0100 Subject: [PATCH] fix tests --- global.json | 4 +- src/functions/assert/Mock/Should-Invoke.ps1 | 48 ++++++++++++------- .../assert/Mock/Should-NotInvoke.ps1 | 36 +++++++------- .../assert/Mock/Should-Invoke.Tests.ps1 | 14 ++++++ .../assert/Mock/Should-NotInvoke.Tests.ps1 | 22 ++++++++- 5 files changed, 84 insertions(+), 40 deletions(-) diff --git a/global.json b/global.json index 2d566bd8c..786bc6556 100644 --- a/global.json +++ b/global.json @@ -1,7 +1,7 @@ { "sdk": { - "rollForward": "latestFeature", + "rollForward": "latestMajor", "version": "8.0.100", "allowPrerelease": false } -} \ No newline at end of file +} diff --git a/src/functions/assert/Mock/Should-Invoke.ps1 b/src/functions/assert/Mock/Should-Invoke.ps1 index c57f972b9..8dc3f388c 100644 --- a/src/functions/assert/Mock/Should-Invoke.ps1 +++ b/src/functions/assert/Mock/Should-Invoke.ps1 @@ -7,13 +7,13 @@ .DESCRIPTION This command verifies that a mocked command has been called a certain number of times. If the call history of the mocked command does not match the parameters - passed to Should -Invoke, Should -Invoke will throw an exception. + passed to Should-Invoke, Should-Invoke will throw an exception. .PARAMETER CommandName The mocked command whose call history should be checked. .PARAMETER ModuleName - The module where the mock being checked was injected. This is optional, + The module where the mock being checked was injected. This is optional, and must match the ModuleName that was used when setting up the Mock. .PARAMETER Times @@ -35,14 +35,14 @@ Like ParameterFilter, except when you use ExclusiveFilter, and there were any calls to the mocked command which do not match the filter, an exception will be thrown. This is a convenient way to avoid needing - to have two calls to Should -Invoke like this: + to have two calls to Should-Invoke like this: - Should -Invoke SomeCommand -Times 1 -ParameterFilter { $something -eq $true } - Should -Invoke SomeCommand -Times 0 -ParameterFilter { $something -ne $true } + Should-Invoke SomeCommand -Times 1 -ParameterFilter { $something -eq $true } + Should-Invoke SomeCommand -Times 0 -ParameterFilter { $something -ne $true } .PARAMETER Scope An optional parameter specifying the Pester scope in which to check for - calls to the mocked command. For RSpec style tests, Should -Invoke will find + calls to the mocked command. For RSpec style tests, Should-Invoke will find all calls to the mocked command in the current Context block (if present), or the current Describe block (if there is no active Context), by default. Valid values are Describe, Context and It. If you use a scope of Describe or @@ -56,29 +56,35 @@ Makes sure that all verifiable mocks were called. .EXAMPLE + ```powershell Mock Set-Content {} {... Some Code ...} - Should -Invoke Set-Content + Should-Invoke Set-Content + ``` This will throw an exception and cause the test to fail if Set-Content is not called in Some Code. .EXAMPLE + ```powershell Mock Set-Content -parameterFilter {$path.StartsWith("$env:temp\")} {... Some Code ...} - Should -Invoke Set-Content 2 { $path -eq "$env:temp\test.txt" } + Should-Invoke Set-Content 2 { $path -eq "$env:temp\test.txt" } + ``` This will throw an exception if some code calls Set-Content on $path=$env:temp\test.txt less than 2 times .EXAMPLE + ```powershell Mock Set-Content {} {... Some Code ...} - Should -Invoke Set-Content 0 + Should-Invoke Set-Content 0 + ``` This will throw an exception if some code calls Set-Content at all @@ -87,51 +93,57 @@ {... Some Code ...} - Should -Invoke Set-Content -Exactly 2 + Should-Invoke Set-Content -Exactly 2 This will throw an exception if some code does not call Set-Content Exactly two times. .EXAMPLE - Describe 'Should -Invoke Scope behavior' { + ```powershell + Describe 'Should-Invoke Scope behavior' { Mock Set-Content { } It 'Calls Set-Content at least once in the It block' { {... Some Code ...} - Should -Invoke Set-Content -Exactly 0 -Scope It + Should-Invoke Set-Content -Exactly 0 -Scope It } } + ``` Checks for calls only within the current It block. .EXAMPLE + ```powershell Describe 'Describe' { Mock -ModuleName SomeModule Set-Content { } {... Some Code ...} It 'Calls Set-Content at least once in the Describe block' { - Should -Invoke -ModuleName SomeModule Set-Content + Should-Invoke -ModuleName SomeModule Set-Content } } + ``` Checks for calls to the mock within the SomeModule module. Note that both the Mock - and Should -Invoke commands use the same module name. + and Should-Invoke commands use the same module name. .EXAMPLE - Should -Invoke Get-ChildItem -ExclusiveFilter { $Path -eq 'C:\' } + ```powershell + Should-Invoke Get-ChildItem -ExclusiveFilter { $Path -eq 'C:\' } + ``` Checks to make sure that Get-ChildItem was called at least one time with the -Path parameter set to 'C:\', and that it was not called at all with the -Path parameter set to any other value. .NOTES - The parameter filter passed to Should -Invoke does not necessarily have to match the parameter filter - (if any) which was used to create the Mock. Should -Invoke will find any entry in the command history + The parameter filter passed to Should-Invoke does not necessarily have to match the parameter filter + (if any) which was used to create the Mock. Should-Invoke will find any entry in the command history which matches its parameter filter, regardless of how the Mock was created. However, if any calls to the mocked command are made which did not match any mock's parameter filter (resulting in the original command being executed instead of a mock), these calls to the original command are not tracked in the call history. - In other words, Should -Invoke can only be used to check for calls to the mocked implementation, not + In other words, Should-Invoke can only be used to check for calls to the mocked implementation, not to the original. .LINK diff --git a/src/functions/assert/Mock/Should-NotInvoke.ps1 b/src/functions/assert/Mock/Should-NotInvoke.ps1 index 72a801e84..8d056dbc5 100644 --- a/src/functions/assert/Mock/Should-NotInvoke.ps1 +++ b/src/functions/assert/Mock/Should-NotInvoke.ps1 @@ -4,9 +4,9 @@ Checks that mocked command was not called and throws exception if it was. .DESCRIPTION - This command verifies that a mocked command has been called a certain number + This command verifies that a mocked command has not been called a certain number of times. If the call history of the mocked command does not match the parameters - passed to Should -Invoke, Should -Invoke will throw an exception. + passed to Should-NotInvoke, Should-NotInvoke will throw an exception. .PARAMETER CommandName The mocked command whose call history should be checked. @@ -34,14 +34,14 @@ Like ParameterFilter, except when you use ExclusiveFilter, and there were any calls to the mocked command which do not match the filter, an exception will be thrown. This is a convenient way to avoid needing - to have two calls to Should -Invoke like this: + to have two calls to Should-NotInvoke like this: - Should -Invoke SomeCommand -Times 1 -ParameterFilter { $something -eq $true } - Should -Invoke SomeCommand -Times 0 -ParameterFilter { $something -ne $true } + Should-NotInvoke SomeCommand -Times 1 -ParameterFilter { $something -eq $true } + Should-NotInvoke SomeCommand -Times 0 -ParameterFilter { $something -ne $true } .PARAMETER Scope An optional parameter specifying the Pester scope in which to check for - calls to the mocked command. For RSpec style tests, Should -Invoke will find + calls to the mocked command. For RSpec style tests, Should-NotInvoke will find all calls to the mocked command in the current Context block (if present), or the current Describe block (if there is no active Context), by default. Valid values are Describe, Context and It. If you use a scope of Describe or @@ -59,7 +59,7 @@ {... Some Code ...} - Should -Invoke Set-Content + Should-NotInvoke Set-Content This will throw an exception and cause the test to fail if Set-Content is not called in Some Code. @@ -68,7 +68,7 @@ {... Some Code ...} - Should -Invoke Set-Content 2 { $path -eq "$env:temp\test.txt" } + Should-NotInvoke Set-Content 2 { $path -eq "$env:temp\test.txt" } This will throw an exception if some code calls Set-Content on $path=$env:temp\test.txt less than 2 times @@ -77,7 +77,7 @@ {... Some Code ...} - Should -Invoke Set-Content 0 + Should-NotInvoke Set-Content 0 This will throw an exception if some code calls Set-Content at all @@ -86,18 +86,18 @@ {... Some Code ...} - Should -Invoke Set-Content -Exactly 2 + Should-NotInvoke Set-Content -Exactly 2 This will throw an exception if some code does not call Set-Content Exactly two times. .EXAMPLE - Describe 'Should -Invoke Scope behavior' { + Describe 'Should-NotInvoke Scope behavior' { Mock Set-Content { } It 'Calls Set-Content at least once in the It block' { {... Some Code ...} - Should -Invoke Set-Content -Exactly 0 -Scope It + Should-NotInvoke Set-Content -Exactly 0 -Scope It } } @@ -110,27 +110,27 @@ {... Some Code ...} It 'Calls Set-Content at least once in the Describe block' { - Should -Invoke -ModuleName SomeModule Set-Content + Should-NotInvoke -ModuleName SomeModule Set-Content } } Checks for calls to the mock within the SomeModule module. Note that both the Mock - and Should -Invoke commands use the same module name. + and Should-NotInvoke commands use the same module name. .EXAMPLE - Should -Invoke Get-ChildItem -ExclusiveFilter { $Path -eq 'C:\' } + Should-NotInvoke Get-ChildItem -ExclusiveFilter { $Path -eq 'C:\' } Checks to make sure that Get-ChildItem was called at least one time with the -Path parameter set to 'C:\', and that it was not called at all with the -Path parameter set to any other value. .NOTES - The parameter filter passed to Should -Invoke does not necessarily have to match the parameter filter - (if any) which was used to create the Mock. Should -Invoke will find any entry in the command history + The parameter filter passed to Should-NotInvoke does not necessarily have to match the parameter filter + (if any) which was used to create the Mock. Should-NotInvoke will find any entry in the command history which matches its parameter filter, regardless of how the Mock was created. However, if any calls to the mocked command are made which did not match any mock's parameter filter (resulting in the original command being executed instead of a mock), these calls to the original command are not tracked in the call history. - In other words, Should -Invoke can only be used to check for calls to the mocked implementation, not + In other words, Should-NotInvoke can only be used to check for calls to the mocked implementation, not to the original. .LINK diff --git a/tst/functions/assert/Mock/Should-Invoke.Tests.ps1 b/tst/functions/assert/Mock/Should-Invoke.Tests.ps1 index 13253ec1c..1abd620ba 100644 --- a/tst/functions/assert/Mock/Should-Invoke.Tests.ps1 +++ b/tst/functions/assert/Mock/Should-Invoke.Tests.ps1 @@ -9,6 +9,13 @@ Describe "Should-Invoke" { Should-Invoke f -Times 1 -Exactly } + + It "Failes when mock was invoked 0 times" { + function f () { } + Mock f + + { Should-Invoke f -Times 1 -Exactly } | Verify-Throw + } } Describe "Should-Invoke -Verifiable" { @@ -20,4 +27,11 @@ Describe "Should-Invoke -Verifiable" { Should-Invoke -Verifiable } + + It "Fails when not all verifiable mocks were invoked" { + function f () { } + Mock f -Verifiable + + { Should-Invoke -Verifiable } | Verify-Throw + } } diff --git a/tst/functions/assert/Mock/Should-NotInvoke.Tests.ps1 b/tst/functions/assert/Mock/Should-NotInvoke.Tests.ps1 index d111201cf..aa9b194c5 100644 --- a/tst/functions/assert/Mock/Should-NotInvoke.Tests.ps1 +++ b/tst/functions/assert/Mock/Should-NotInvoke.Tests.ps1 @@ -7,13 +7,31 @@ Describe "Should-Invoke" { Should-Invoke f -Times 1 -Exactly } + + It "Fails when Mock was invoked" { + function f () { } + Mock f + + f + + { Should-Invoke f -Times 1 -Exactly } | Verify-Throw + } } -Describe "Should-Invoke -Verifiable" { +Describe "Should-NotInvoke -Verifiable" { It "Passes when no verifiable mocks were invoked" { function f () { } Mock f -Verifiable - Should-Invoke -Verifiable + Should-NotInvoke -Verifiable + } + + It "Fails when verifiable mocks were invoked" { + function f () { } + Mock f -Verifiable + + f + + { Should-NotInvoke -Verifiable } | Verify-Throw } }