2017-01-17 1 views
1

クラスベースのDSCリソースのユニットテストに問題があります。私はクラスのいくつかの関数を模擬しようとしていると私はキャストエラーを取得します。ユニットPesterでクラスベースの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 
     } 
    } 
} 

誰もがこれを達成する方法を知っていますか?

ありがとうございます。

+0

リソースの見た目はわかりませんが、最初のアイデアはコード内の 'InModuleScope xVMWareVM {}'ですか? – BartekB

答えて

2

現在、Pesterを使用してクラス機能をモックすることはできません。現在の回避策は、関数を置き換えるためにAdd-Member -MemberType ScriptMethodを使用することです。これはあなたが模倣を主張することはありませんことを意味します。

DockerDsc tests by @bgelensに対してこれを借りました。

クラスコードがないと、私はこれをテストすることができませんでしたが、上記の@bgelensコードとともに考えてください。

using module 'C:\Program Files\WindowsPowerShell\Modules\xVMWareVM\xVMWareVM.psm1' 

    Describe "Set" { 

    Context "If the VM does not exist" { 
     $resource = [xVMWareVM]::new() 
     $global:CreateVmCalled = 0 
     $resource = $resource | 
      Add-Member -MemberType ScriptMethod -Name TestVMExists -Value { 
        return $false 
       } -Force -PassThru 
     $resource = $resource | 
      Add-Member -MemberType ScriptMethod -Name CreateVM -Value { 
        $global:CreateVmCalled ++ 
       } -Force -PassThru 

     It "Calls Create VM once" { 
      $global:CreateVmCalled | should be 1 
     } 
    } 
} 
+1

ありがとう、これはまさに私が欲しいものです:) – Carl

関連する問題