Created
          October 15, 2017 22:40 
        
      - 
      
- 
        Save alx9r/fcf7b61453ec730420b179024af6aa75 to your computer and use it in GitHub Desktop. 
    Verify function experiment for use in Pester's Assert-MockCalled
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | function Verify | |
| { | |
| param | |
| ( | |
| [Parameter(ValueFromPipeline, | |
| Position = 1, | |
| Mandatory)] | |
| $ExpressionResult | |
| ) | |
| process | |
| { | |
| if ( $ExpressionResult -isnot [bool] ) | |
| { | |
| throw "Verify failed: Expected $ExpressionResult to be boolean not $($ExpressionResult.GetType())." | |
| } | |
| if ( -not $ExpressionResult ) | |
| { | |
| throw "Verify failed", | |
| "$($PSCmdlet.MyInvocation.Line)" -join [System.Environment]::NewLine | |
| } | |
| $ExpressionResult | |
| } | |
| } | |
| Describe 'verify' { | |
| function f { param($a,$b) } | |
| Mock f -Verifiable | |
| Context 'success' { | |
| f -a 1 -b 2 | |
| It 'several subexpressions' { | |
| Assert-MockCalled f 1 { | |
| ($a -eq 1 | Verify) -and | |
| ($b -eq 2 | Verify) -and | |
| ($b-1 -eq $a | Verify) | |
| } | |
| } | |
| } | |
| Context 'error message in Assert-MockCalled' { | |
| f -a 1 -b 2 | |
| It 'single expression' { | |
| Assert-MockCalled f 1 { | |
| $a -eq 2 | Verify | |
| } | |
| } | |
| It 'multiple expressions, first line' { | |
| Assert-MockCalled f 1 { | |
| ($a -eq 2 | Verify) -and | |
| ($b -eq 2 | Verify) -and | |
| ($b-1 -eq $a | Verify) | |
| } | |
| } | |
| It 'multiple expressions, second line' { | |
| Assert-MockCalled f 1 { | |
| ($a -eq 1 | Verify) -and | |
| ($b -eq 1 | Verify) -and | |
| ($b-1 -eq $a | Verify) | |
| } | |
| } | |
| It 'multiple expressions, third line' { | |
| Assert-MockCalled f 1 { | |
| ($a -eq 1 | Verify) -and | |
| ($b -eq 2 | Verify) -and | |
| ($b -eq $a | Verify) | |
| } | |
| } | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment