【发布时间】:2021-11-23 17:21:22
【问题描述】:
我想模拟 Get-ChildItem 的结果,但不断遇到问题。如何在 Pester 中模拟?
It 'Test get latest version' {
Mock -CommandName 'Test-Path' –MockWith {
return $true
}
Mock -CommandName 'Get-ChildItem' –MockWith {
$MockedListOfDirectories = `
'test_1.0.1.1', `
'test_1.1.10.5', `
'test_1.1.10.1', `
'test_1.2.18.1', `
'test_1.4.7.0'
return $MockedListOfDirectories
}
}
测试的输出:
PSInvalidCastException: Cannot convert the "â€MockWith {
return True
}
Mock -CommandName 'Get-ChildItem' â€MockWith" value of type "System.String" to type "System.Management.Automation.ScriptBlock".
ArgumentTransformationMetadataException:
Cannot convert the "â€MockWith {
return True
}
Mock -CommandName 'Get-ChildItem' â€MockWith" value of type "System.String" to type "System.Management.Automation.ScriptBlock".
ParameterBindingArgumentTransformationException: Cannot process argument transformation on parameter 'MockWith'. Cannot convert the "â€MockWith {
return True
}
Mock -CommandName 'Get-ChildItem' â€MockWith" value of type "System.String" to type "System.Management.Automation.ScriptBlock".
at <ScriptBlock>, C:\my\path\to\file.ps1:8
【问题讨论】:
标签: powershell pester