2017-12-06 14 views
2

こんにちはすべて、が原因Scriptブロック

に100%せがむコードカバレッジに到達することはできません私が原因スクリプトブロックに100%のコードカバレッジに到達していないので、私のせびるの長い時間を苦労してきました。私は研究を続けていて、記事を読んで成功していないし、あなたからの助けを求めることに決めました。 :)

説明したいコードの一部にスクリプトブロックが含まれており、後でコマンドレットのInvoke-Commandに渡されます。以下のサンプルコードは:

function Get-Function { 
..... 
Set-Alias Start-PowerShell32 $env:computername\WindowsPowerShell\v1.0\powershell.exe" 
$ScriptBlock = { 
    Start-PowerShell32 -noprofile { 
     $sample = Get-Content -Path "C:\sample.txt 
     Import-Module "C:\Program Files (x86)\Software" 

     @($sample) | Get-Service | Where-Object { $_.Status -eq 'Stopped' } 
     } 
    } 
Invoke-Command -ScriptBlock $ScriptBlock 
} 

Describe Get-Function { 
.... 
function Get-Statistics { 

    Start-PowerShell32 -noprofile { 
     $sample = Get-Content -Path "C:\sample.txt 
     Import-Module "C:\Program Files (x86)\Software" 

     @($sample) | Get-Service | Where-Object { $_.Status -eq 'Stopped' } 
     } 
} 
Context '1st Context'{ 
mock Set-Alias {'Setting alias for Powershell 32 bit'} -Verifiable 
mock Get-Statistics {'Getting Statistics ....'} -Verifiable 
mock Invoke-Command {Get-Statistics} -Verifiable 
$result = Get-Function 

    it 'should return etting alias for Powershell 32 bit'{ 
     $result[0] | should be "Setting alias for Powershell 32 bit" 
     } 
    it 'should return Getting Mailbox Statistics ....'{ 
     $result[1] | should be "Getting Statistics ...." 
     } 
    it 'should call all verifiable mocks'{ 
     Assert-VerifiableMocks 
     } 
    } 
} 

私は私のせがむにやったことは、私は内部のカスタム関数を作ったということである私のブロックを説明し(取得-統計)それはいつでも呼び出されるため、基本的にはスクリプトブロックの内側にあります私はInvoke-CommandをGet-Statisticsにしようとします。 My Pesterは成功しますが、コードカバレッジでは100%は得られません。 あなたは私にこのことを教えることができますか?テストを変更する必要がありますか? はあなたのDescribe {ブロックの先頭で、あなたに

+1

あなたは '起動-Command'をからかっする必要があります。 – TheIncorrigible1

+0

こんにちは 私は最初のContext内で私のinvoke-commandを嘲笑しました。 –

+0

あなたのモックが悪いです。 100%のカバレッジを達成することが不可能になるように、内部スクリプトブロックを呼び出すことは決してありません。 – TheIncorrigible1

答えて

0

ありがとう、Invoke-Commandのモックを含める:

Describe '..' { 
    Mock Invoke-Command { Param($SB) . $SB } 

    ... 
} 
+0

うわー、先生のおかげです。私は上記の回避策を試していましたが、今では私の授乳期で100%のコードカバレッジを達成することができます。 –

関連する問題