Skip to content

Instantly share code, notes, and snippets.

@brantb
Last active September 27, 2017 13:39
Show Gist options
  • Save brantb/2ee6bc26a2a98eb74fa729a76f636b9b to your computer and use it in GitHub Desktop.
Save brantb/2ee6bc26a2a98eb74fa729a76f636b9b to your computer and use it in GitHub Desktop.
rmo Pester -Force
ipmo Pester -MaximumVersion '4.0.8'
Invoke-Pester .\sut.ps1
rmo Pester -Force
ipmo Pester -MaximumVersion '3.6.0'
Invoke-Pester .\sut.ps1
<# Output:
Executing all tests in '.\sut.ps1'
Executing script .\sut.ps1
Describing Pester version 4.0.8
[+] Assert-MockCalled fails when -ParameterFilter returns nothing 87ms
[+] Assert-MockCalled fails when -ParameterFilter explicitly returns $null 16ms
[-] Assert-MockCalled passes when -ParameterFilter contains an assertion but returns nothing 50ms
Expected: the expression not to throw an exception. Message was {Expected Invoke-SomethingInteresting to be called at least 1 times but was called 0 times}
from C:\Users\brant.bobby\Documents\WindowsPowerShell\Modules\Pester\4.0.8\Functions\Mock.ps1:674 char:9
+ throw ( New-ShouldErrorRecord -Message $failureMessage -Line ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
30: } | Should Not Throw
at Invoke-LegacyAssertion, C:\Users\brant.bobby\Documents\WindowsPowerShell\Modules\Pester\4.0.8\Functions\Assertions\Should.ps1: line 190
at <ScriptBlock>, C:\Users\brant.bobby\Desktop\sut.ps1: line 26
[+] Assert-MockCalled passes when -ParameterFilter performs an assertion and explicitly returns $true 21ms
Tests completed in 176ms
Tests Passed: 3, Failed: 1, Skipped: 0, Pending: 0, Inconclusive: 0
Describing Pester version 3.4.0
[+] Assert-MockCalled fails when -ParameterFilter returns nothing 53ms
[+] Assert-MockCalled fails when -ParameterFilter explicitly returns $null 13ms
[+] Assert-MockCalled passes when -ParameterFilter contains an assertion but returns nothing 22ms
[+] Assert-MockCalled passes when -ParameterFilter performs an assertion and explicitly returns $true 11ms
Tests completed in 101ms
Passed: 4 Failed: 0 Skipped: 0 Pending: 0 Inconclusive: 0
#>
$pesterVersion = (Get-Module Pester).Version
# Just an arbitrary cmdlet which we will mock
function Invoke-SomethingInteresting
{
[CmdletBinding()]param($Thing)
$Thing
}
Describe "Pester version $pesterVersion" {
Mock Invoke-SomethingInteresting {}
Invoke-SomethingInteresting -Thing "foo"
It "Assert-MockCalled fails when -ParameterFilter returns nothing" {
{ Assert-MockCalled Invoke-SomethingInteresting -ParameterFilter {} } | Should Throw
}
It "Assert-MockCalled fails when -ParameterFilter explicitly returns `$null" {
{
Assert-MockCalled Invoke-SomethingInteresting -ParameterFilter { return $null }
} | Should Throw
}
It "Assert-MockCalled passes when -ParameterFilter contains an assertion but returns nothing" {
{
Assert-MockCalled Invoke-SomethingInteresting -ParameterFilter {
$Thing | Should Be "foo"
}
} | Should Not Throw
}
It "Assert-MockCalled passes when -ParameterFilter performs an assertion and explicitly returns `$true" {
{
Assert-MockCalled Invoke-SomethingInteresting -ParameterFilter {
$Thing | Should Be "foo"
return $true
}
} | Should Not Throw
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment