以下似乎工作正常:
Test.ps1 - 这仅包含两个函数。两者都采用相同的参数,但Test-Call
调用到Mocked-Call
. 我们将在测试中模拟Mocked-Call
。
Function Test-Call {
param(
$text,
[switch]$switch
)
Mocked-Call $text -switch:$switch
}
Function Mocked-Call {
param(
$text,
[switch]$switch
)
$text
}
Test.Tests.ps1 - 这是我们实际的测试脚本。请注意,我们有两个模拟实现Mocked-Call
。当switch
参数设置为true时,第一个将匹配。当text
参数的值为第四个 并且参数的switch
值为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"
}
}
运行测试显示绿色:
Describing Test-Call
[+] mocks switch parms 17ms
Tests completed in 17ms
Passed: 1 Failed: 0