forked from pester/Pester
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
198 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
TOPIC | ||
Mocking | ||
|
||
SYNOPSIS | ||
Pester provides a set of Mocking functions making it easy to fake dependencies | ||
and also to verify behavior. Using these mocking functions can allow you to | ||
"shim" a data layer or mock other complex functions that already have their | ||
own tests. | ||
|
||
DESCRIPTION | ||
With the set of Mocking functions that Pester exposes, one can: | ||
|
||
- Mock the behavior of ANY powershell command. | ||
- Verify that specific commands were (or were not) called. | ||
- Verify the number of times a command was called with a set of specified | ||
parameters. | ||
|
||
MOCKING FUNCTIONS | ||
See Get-Help for any of the below functions for more detailed information. | ||
|
||
Mock | ||
Mocks the behavior of an existing command with an alternate | ||
implementation. | ||
|
||
Assert-VerifiableMocks | ||
Checks if any Verifiable Mock has not been invoked. If so, this will | ||
throw an exception. | ||
|
||
Assert-MockCalled | ||
Checks if a Mocked command has been called a certain number of times | ||
and throws an exception if it has not. | ||
|
||
EXAMPLE | ||
function BuildIfChanged { | ||
$thisVersion=Get-Version | ||
$nextVersion=Get-NextVersion | ||
if($thisVersion -ne $nextVersion) {Build $nextVersion} | ||
return $nextVersion | ||
} | ||
|
||
$here = Split-Path -Parent $MyInvocation.MyCommand.Path | ||
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") | ||
. "$here\$sut" | ||
|
||
Describe "BuildIfChanged" { | ||
Context "Wnen there are Changes" { | ||
Mock Get-Version {return 1.1} | ||
Mock Get-NextVersion {return 1.2} | ||
Mock Build {} -Verifiable -ParameterFilter {$version -eq 1.2} | ||
|
||
$result = BuildIfChanged | ||
|
||
It "Builds the next version" { | ||
Assert-VerifiableMocks | ||
} | ||
It "returns the next version number" { | ||
$result.Should.Be(1.2) | ||
} | ||
} | ||
Context "Wnen there are no Changes" { | ||
Mock Get-Version -MockWith {return 1.1} | ||
Mock Get-NextVersion -MockWith {return 1.1} | ||
Mock Build -MockWith {} | ||
|
||
$result = BuildIfChanged | ||
|
||
It "Should not build the next version" { | ||
Assert-MockCalled Build -Times 0 -ParameterFilter{$version -eq 1.1} | ||
} | ||
} | ||
} | ||
|
||
SEE ALSO | ||
Mock | ||
Assert-VerifiableMocks | ||
Assert-MockCalled | ||
Describe | ||
Context | ||
It |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters