【发布时间】:2017-01-17 17:32:54
【问题描述】:
我在对基于类的 DSC 资源进行单元测试时遇到问题。我正在尝试模拟类中的几个函数,但出现转换错误。
PSInvalidCastException: Cannot convert the "bool TestVMExists(string vmPath,
string vmName)" value of type "System.Management.Automation.PSMethod" to type
"System.Management.Automation.ScriptBlock".
我的测试代码是这样的:
using module 'C:\Program Files\WindowsPowerShell\Modules\xVMWareVM\xVMWareVM.psm1'
$resource = [xVMWareVM]::new()
Describe "Set" {
Context "If the VM does not exist" {
Mock xVMWareVM $resource.TestVMExists {return $false}
Mock xVMWareVM $resource.CreateVM
It "Calls Create VM once" {
Assert-MockCalled $resource.CreateVM -Times 1
}
}
}
有谁知道如何做到这一点?
提前致谢
【问题讨论】:
-
不确定资源是什么样子,但首先想到的是:
InModuleScope xVMWareVM { }围绕代码?
标签: unit-testing powershell dsc pester