Skip to content

Commit

Permalink
Update offline mocking docs
Browse files Browse the repository at this point in the history
  • Loading branch information
fflaten committed Jun 9, 2024
1 parent 8f1036e commit aa7f319
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions src/en-US/about_Mocking.help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ LONG DESCRIPTION
Mocks the behavior of an existing command with an alternate
implementation.

Assert-VerifiableMock
Should -InvokeVerifiable
Checks if any Verifiable Mock has not been invoked. If so, this will
throw an exception.

Assert-MockCalled
Should -Invoke
Checks if a Mocked command has been called a certain number of times
and throws an exception if it has not.

Expand Down Expand Up @@ -55,7 +55,7 @@ LONG DESCRIPTION
$result = BuildIfChanged

It "Builds the next version" {
Assert-VerifiableMock
Should -InvokeVerifiable
}
It "returns the next version number" {
$result | Should -Be 1.2
Expand All @@ -69,7 +69,7 @@ LONG DESCRIPTION
$result = BuildIfChanged

It "Should not build the next version" {
Assert-MockCalled Build -Times 0 -ParameterFilter {$version -eq 1.1}
Should -Invoke Build -Times 0 -ParameterFilter {$version -eq 1.1}
}
}
}
Expand Down Expand Up @@ -123,7 +123,7 @@ LONG DESCRIPTION
$result = BuildIfChanged

It "Builds the next version and calls Write-Host" {
Assert-VerifiableMock
Should -InvokeVerifiable
}

It "returns the next version number" {
Expand All @@ -139,15 +139,15 @@ LONG DESCRIPTION
$result = BuildIfChanged

It "Should not build the next version" {
Assert-MockCalled Build -ModuleName MyModule -Times 0 -ParameterFilter {
Should -Invoke Build -ModuleName MyModule -Times 0 -ParameterFilter {
$version -eq 1.1
}
}
}
}


In this sample test script, all calls to Mock and Assert-MockCalled have the
In this sample test script, all calls to Mock and Should -Invoke have the
-ModuleName MyModule parameter added. This tells Pester to inject the mock into the module scope,
which causes any calls to those commands from inside the module to execute the mock instead.

Expand All @@ -168,21 +168,20 @@ LONG DESCRIPTION
Build $testVersion

It 'Outputs the correct message' {
Assert-MockCalled Write-Host -ParameterFilter {
Should -Invoke Write-Host -ParameterFilter {
$Object -eq "a build was run for version: $testVersion"
}
}
}
}

When using InModuleScope, you no longer need to specify a ModuleName parameter when calling
Mock or Assert-MockCalled for commands in the module. You can also directly call the Build
Mock or Should -Invoke for commands in the module. You can also directly call the Build
function that the module does not export.

SEE ALSO
Mock
Assert-VerifiableMock
Assert-MockCalled
Should
InModuleScope
Describe
Context
Expand Down

0 comments on commit aa7f319

Please sign in to comment.