我现在正在使用美妙的Pester单元测试缓慢学习一段时间。我有点拘泥于检查我的函数是否可以运行“如果没有提供任何强制性输入到函数”的用法。这里给我一盏红灯,想要获得绿色测试结果并继续进行编码。Pester单元测试功能必选= True
所以我有一个功能如下。
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的克服某些类型的消息响应并转化为单元测试这个...
['应该'测试投掷和不投掷需要一个脚本块作为输入](https://github.com/pester/Pester/wiki/Should#throw)所以尝试'{Code} |应该投掷吗? – TessellatingHeckler
哦,你知道什么...它的作品!谢谢你@TessellatingHeckler –