The following appears to work fine:
Test.ps1 - This just contains two functions. Both take the same parameters but Test-Call
calls through to Mocked-Call
. We will mock Mocked-Call
in our tests.
Function Test-Call {
param(
$text,
[switch]$switch
)
Mocked-Call $text -switch:$switch
}
Function Mocked-Call {
param(
$text,
[switch]$switch
)
$text
}
Test.Tests.ps1 - This is our actual test script. Note we have two mock implementations for Mocked-Call
. The first will match when the switch
parameter is set to true. The second will match when the text
parameter has a value of fourth and the switch
parameter has a value of false.
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".")
. "$here\$sut"
Describe "Test-Call" {
It "mocks switch parms" {
Mock Mocked-Call { "mocked" } -parameterFilter { $switch -eq $true }
Mock Mocked-Call { "mocked again" } -parameterFilter { $text -eq "fourth" -and $switch -eq $false }
$first = Test-Call "first"
$first | Should Be "first"
$second = Test-Call "second" -switch
$second | Should Be "mocked"
$third = Test-Call "third" -switch:$true
$third | Should Be "mocked"
$fourth = Test-Call "fourth" -switch:$false
$fourth | Should Be "mocked again"
}
}
Running the tests shows green:
Describing Test-Call
[+] mocks switch parms 17ms
Tests completed in 17ms
Passed: 1 Failed: 0