【发布时间】:2022-04-25 13:00:31
【问题描述】:
我正在尝试使用 Pester for PowerShell 来测试我的一些代码,但我无法让 Pester 解决错误。
以这个非常基本的例子-
using module AccessTokenRequestModel
InModuleScope -ModuleName AccessTokenRequestModel -ScriptBlock {
### Create a new instance of the 'AccessTokenRequest' object.
$request = [AccessTokenRequest]::new()
Describe -Name "the 'AccessTokenRequest' module -" -Tags @("AccessTokenRequest","Get","Unit") -Fixture {
It "Given a valid organisation, the 'GetAccessToken' method should return a valid Access Token entity." {
$accessTokenEntity = $request.GetAccessToken("ValidOrg")
$accessTokenEntity.PartitionKey | Should be "AccessToken"
$accessTokenEntity.RowKey | Should be "ValidOrg"
$accessTokenEntity.AccessToken | Should be "12345"
}
It "Given an invalid organisation, the 'GetAccessToken' method should throw an error of type 'AccessTokenNotFoundException.'" {
$request.GetAccessToken("FakeOrg") | Should -Throw
}
}
}
对 $tokens.GetAccessToken("FakeOrg") 的调用导致抛出 AccessTokenNotFoundException 类型的错误,但 Pester 测试失败。
Describing the 'AccessTokenRequest' module -
[+] Given a valid organisation, the 'GetAccessToken' method should return a valid Access Token entity. 70ms
[-] Given an invalid organisation, the 'GetAccessToken' method should throw an error of type 'AccessTokenNotFoundException.' 61ms
AccessTokenNotFoundException: Access Token for organisation 'NonExistentAccessTokenTest' does not exist.
at GetAccessTokenEntity, C:\Users\dgard\OneDrive - Landmark Information Group Ltd\Function Apps\AzureDevOpsVariableChecker\Modules\AccessTokenService\AccessTokenService.psm1: line 73
at GetAccessToken, C:\Users\dgard\OneDrive - Landmark Information Group Ltd\Function Apps\AzureDevOpsVariableChecker\Modules\AccessTokenRequestModel\AccessTokenRequestModel.psm1: line 25
at <ScriptBlock>, C:\Users\dgard\OneDrive - Landmark Information Group Ltd\Function Apps\AzureDevOpsVariableChecker\Tests\Unit\AccessTokenRequest.Tests.ps1: line 42
错误是由throw 命令生成的,因此是终止错误,正如this question 中所建议的那样。除非我误解了documentation,否则它表明由should -throw 评估的抛出错误应该通过。
我在这里缺少什么 - 抛出错误时如何使此测试通过?
【问题讨论】:
标签: powershell powershell-core pester