locked
Pester assert failed RRS feed

  • Question

  • Hi experts,

    I'm writing a PowerShell function that imports System.Security.SecureString from the pre-exported 'Standard Encrypted String' and 'Encryption Key', and I ran into an issue when I was writing Pester test for it. Here is the simplified example:

    function Import-SecureString ($str, $key) {
        ConvertTo-SecureString -String $str -Key $key
    }
    
    Describe 'Import-SecureString' {
        BeforeAll {
            $password = '123456'
            $encryptionKey = New-Object -TypeName System.Byte[] -ArgumentList 24
            $RNGCryptoServiceProvider = New-Object -TypeName System.Security.Cryptography.RNGCryptoServiceProvider
            $RNGCryptoServiceProvider.GetBytes($encryptionKey)
            $encryptedStandardString = ConvertTo-SecureString -String $password -AsPlainText -Force | ConvertFrom-SecureString -Key $encryptionKey
        }
    
        Context 'Unit Test' {
            Mock ConvertTo-SecureString
    
            Import-SecureString -str $encryptedStandardString -key $encryptionKey
    
            It 'Should bind VAR $encryptedStandardString to PARAM str' {
                $assert = @{
                    CommandName = 'ConvertTo-SecureString'
                    Times = 1
                    Exactly = $true
                    ParameterFilter = {$String -eq $encryptedStandardString}
                }
                Assert-MockCalled @assert
            }
    
            It 'Should bind VAR $encryptionKey to PARAM key' {
                $assert = @{
                    CommandName = 'ConvertTo-SecureString'
                    Times = 1
                    Exactly = $true
                    ParameterFilter = {$Key -eq $encryptionKey}
                }
                Assert-MockCalled @assert
            }
        }
    }

    In this example, the first 'It' block passed and the second failed, how can I correctly assert the second one?
    Monday, March 18, 2019 2:58 AM

All replies