一段时间以来,我正在慢慢学习使用 Powershell 的精彩 Pester 单元测试。我有点纠结于检查我的函数是否可以运行“如果没有向函数提供任何强制性输入”。这给了我一个红灯,并希望获得绿色测试结果并继续编码。
所以我有一个功能如下。
function Code()
{
param(
[parameter(Mandatory=$true)]
[string]$SourceLocation)
return "Hello from $SourceLocation"
}
我的测试脚本执行以下检查...
$moduleName = 'Code';
Describe $moduleName {
Context "$Function - Returns a result " {
It "does something useful with just $Function function name" {
$true | Should Be $true
}
}
Context "$Function - Returns with no input " {
It "with no input returns Mandatory value expected" {
Code | Should Throw
}
}
Context "$Function - Returns some output" {
It "with a name returns the standard phrase with that name" {
Code "Venus" | Should Be "Hello from Venus"
}
It "with a name returns something that ends with name" {
Code "Mars" | Should Match ".*Mars"
}
}
} #End Describe
我从 AppVeyor 的输出显示了这个结果,其中 [+] 是绿色,[-] 是红色,这是我尽我所能避免的。
Describing Code
Context Code - Returns a result
[+] does something useful with just Code function name 16ms
Context Code - Returns with no input
[-] with no input returns Mandatory value expected 49ms
Cannot process command because of one or more missing mandatory parameters: SourceLocation.
at <ScriptBlock>, C:\projects\code\Code.Tests.ps1: line 117
117: Code | Should Throw
Context Code - Returns some output
[+] with a name returns the standard phrase with that name 23ms
[+] with a name returns something that ends with name 11ms
感谢您提供任何帮助,因为我希望那里有一个绿色条件,因为我不确定如何克服来自 Powershell 的某些类型的消息响应并将其转换为单元测试......