我正在嘗試在 Powershell 中為簡單的 Azure 函式撰寫一個單元
function Get-AzureBlobStorage {
param (
[Parameter(Mandatory)]
[string]$ContainerName,
[Parameter(Mandatory)]
[string]$Blob,
[Parameter(Mandatory)]
$Context
)
try {
return (Get-AzStorageBlob -Container $ContainerName -Context $Context -Blob $Blob)
}
catch {
Write-Error "Blobs in Container [$ContainerName] not found"
}
Unit test
Context 'Get-AzureBlobStorage' {
It 'Should be able to get details to Blob Storage account without any errors' {
$ContainerName = 'test'
$Blob="test-rg"
$Context = "test"
Mock Get-AzStorageBlob { } -ModuleName $moduleName
Get-AzureBlobStorage -ContainerName $ContainerName -Blob $Blob -Context $Context -ErrorAction SilentlyContinue -ErrorVariable errors
$errors.Count | Should -Be 0
}
}
但我無法讓它作業。我收到以下錯誤,
Cannot process argument transformation on parameter 'Context'. Cannot convert the "test" value of type "System.String" to type "Microsoft.Azure.Commands.Common.Authentication.Abstractions.IStorageContext".
我的問題是如何獲得背景關系等值。我還有其他幾個函式,其中一個引數總是一些復雜的物件。為此類功能撰寫單元測驗的最佳方法是什么
uj5u.com熱心網友回復:
您的問題是因為Get-AzStorageBlobcmdlet的輸入需要-Context. 您可以使用Mockwith讓 Pester 洗掉輸入中的強型別-RemoveParameterType。
這是我測驗您的功能的方法:
Describe 'Tests' {
Context 'Get-AzureBlobStorage returns blob' {
BeforeAll {
Mock Get-AzStorageBlob {} -RemoveParameterType Context
}
It 'Should be able to get details to Blob Storage account without any errors' {
$ContainerName = 'test'
$Blob = "test-rg"
$Context = "test"
Get-AzureBlobStorage -ContainerName $ContainerName -Blob $Blob -Context $Context -ErrorVariable errors
Assert-MockCalled Get-AzStorageBlob
}
}
Context 'Get-AzureBlobStorage returns error' {
BeforeAll {
Mock Get-AzStorageBlob { throw 'Error' } -RemoveParameterType Context
Mock Write-Error { }
}
It 'Should return an error via Write-Error' {
$ContainerName = 'test'
$Blob = "test-rg"
$Context = "test"
Get-AzureBlobStorage -ContainerName $ContainerName -Blob $Blob -Context $Context
Assert-MockCalled Write-Error -Times 1 -Exactly
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/434577.html
