我現在正在使用美妙的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 –