2017-07-01 9 views
1

Powershellの素晴らしいPesterユニットテストをしばらく使用して、私はゆっくりと習っています。関数に必須の入力がない場合、関数を実行できるかどうかのチェックの使い方については、まあまあです。私はここで赤い光を与えていて、緑のテスト結果を達成し、コーディングを進めたいと思っていました。Pesterユニットのテスト機能がMandatory = Trueの場合

私は次のような機能を持っています。私のテストスクリプトは、次のチェック で実行され

function Code() 
{  
param(
    [parameter(Mandatory=$true)] 
    [string]$SourceLocation) 
return "Hello from $SourceLocation" 
} 

...

$moduleName = 'Code'; 
Describe $moduleName {   
     Context "$Function - Returns a result " { 
      It "does something useful with just $Function function name" { 
      $true | Should Be $true 
      } 
     } 

     Context "$Function - Returns with no input " { 
     It "with no input returns Mandatory value expected" { 
      Code | Should Throw 
     } 
     } 

     Context "$Function - Returns some output" { 
      It "with a name returns the standard phrase with that name" { 
       Code "Venus" | Should Be "Hello from Venus" 
      } 
      It "with a name returns something that ends with name" { 
       Code "Mars" | Should Match ".*Mars" 
      } 
     } 

    } #End Describe 

AppVeyorから私の出力は、[+]緑色であり、この結果を示し、[ - ]は赤色であるが私はできる限り最善を尽くしています。私はPowerShellのからのメッセージ応答の特定の種類を克服し、ユニットテストにこれを翻訳するかどうかはわかりませんとそこに緑色の条件を希望としてすべてのヘルプは高く評価され

Describing Code 
    Context Code - Returns a result 
     [+] does something useful with just Code function name 16ms 
    Context Code - Returns with no input 
     [-] with no input returns Mandatory value expected 49ms 
     Cannot process command because of one or more missing mandatory parameters: SourceLocation. 
     at <ScriptBlock>, C:\projects\code\Code.Tests.ps1: line 117 
     117:   Code | Should Throw 

    Context Code - Returns some output 
     [+] with a name returns the standard phrase with that name 23ms 
     [+] with a name returns something that ends with name 11ms 

...パー

+2

[ThrowとNot Throwのテストはスクリプトブロックを入力する必要があります](https://github.com/pester/wiki/Should#throw)ので、 '{Code} |投げるべきである ' – TessellatingHeckler

+0

あなたは何を知っていますか...それは動作します!ありがとう@TessellatingHeckler –

答えて

2

TessellatingHecklerからのコメント、あなたのコードが動作していないので、Throwをテストするために、あなたはパイプにShouldを必要とするスクリプトブロック{ }コマンドレット:ときTESTINそれは(しかしそれを注目に値します

{Code} | Should Throw 

をPowerShellが非対話型コンソール(PowerShell.exe -noninteractive)で実行されているため、これはAppVeyorで正常に動作します。 Pesterテストをローカルで実行しようとすると、入力を求めるプロンプトが表示されたときにテストが中断されるように見えます。この周りにいくつかの方法があります

は、一つは単に非対話型モードでPowerShellを使用してローカルでテストを実行することです:

PowerShell.exe -noninteractive {Invoke-Pester} 

もう一つは、警告と($null明示的または空の値を、パラメータを渡すことですあなたが実際に)$nullを受け入れ、このソリューションは、他のすべてのパラメータの型と必ずしも動作しません必須の文字列パラメータを持つことができます。

It "with no input returns Mandatory value expected" { 
    {Code $null} | Should Throw 
} 

これら二つの解決策ということは注目に値するが別の例外メッセージを投げます。さらに、Throwの明示的なメッセージをテストして、コードが何らかの理由で失敗した場合にテストが合格しないようにする必要があります。例:

はあなたのパラメータので、これは、この特定のシナリオのための唯一の複雑な問題である要約すると、それがnull $

It "with no input returns Mandatory value expected" { 
    {Code $null} | Should Throw "Cannot bind argument to parameter 'SourceLocation' because it is an empty string." 
} 

を渡す-noninteractive

It "with no input returns Mandatory value expected" { 
    {Code} | Should Throw 'Cannot process command because of one or more missing mandatory parameters: SourceLocation.' 
} 

を実行しますあなたはその不在をテストしています。例外の

テストは、一般的に単純なプロセスです:

{ some code } | should Throw 'message' 

そして両方の対話型と非対話型のコンソールで正常に動作します。

関連する問題