Pester assert failed

Welcome Forums Pester Pester assert failed

  • This topic has 8 replies, 5 voices, and was last updated 1 year ago by
    Participant
    .
Viewing 8 reply threads
  • Author
    Posts
    • #144974
      Participant
      Topics: 1
      Replies: 2
      Points: 1
      Rank: Member

      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?

    • #145196
      Senior Moderator
      Topics: 9
      Replies: 1243
      Points: 4,475
      Helping Hand
      Rank: Community Hero

      Can you share the pester output for the failed test case.

    • #145238
      Participant
      Topics: 1
      Replies: 2
      Points: 1
      Rank: Member

      Hi kvprasoon,

      Here is the output:

      Describing Import-SecureString
      
        Context Unit Test
          [+] Should bind VAR $encryptedStandardString to PARAM str 5ms
          [-] Should bind VAR $encryptionKey to PARAM key 8ms
            Expected ConvertTo-SecureString to be called 1 times exactly but was called 0 times
            36:             Assert-MockCalled @assert
      

      Also, if I replace the key with “123”, although the key length is invalid, the test will pass:

      function Import-SecureString ($str, $key) {
          ConvertTo-SecureString -String $str -Key $key
      }
      
      Describe 'Import-SecureString' {
          BeforeAll {
              $encryptedStandardString = ConvertTo-SecureString -String '123456' -AsPlainText -Force | ConvertFrom-SecureString
              $encryptionKey = '123'
          }
      
          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
              }
          }
      }
      
    • #145607
      Senior Moderator
      Topics: 9
      Replies: 1243
      Points: 4,475
      Helping Hand
      Rank: Community Hero

      not getting any clue here, will include Jakub here.

    • #146636
      Participant
      Topics: 1
      Replies: 2
      Points: 1
      Rank: Member

      Hi kvprasoon,

      Should I submit an issue to GitHub?

    • #146643
      Senior Moderator
      Topics: 9
      Replies: 1243
      Points: 4,475
      Helping Hand
      Rank: Community Hero

      That’ll be better.

    • #148865
      Participant
      Topics: 0
      Replies: 1
      Points: 7
      Rank: Member

      same problem i faced

    • #161505
      Participant
      Topics: 0
      Replies: 8
      Points: 46
      Helping Hand
      Rank: Member

      Can you share the pester output for the failed test case.

    • #161943
      Participant
      Topics: 0
      Replies: 28
      Points: -7
      Rank: Member

      Both tests pass on my computer (macOS / Pwsh 6.2.1 / Pester 4.8.1), can everybody else replicate it? What versions are you using?

Viewing 8 reply threads
  • The topic ‘Pester assert failed’ is closed to new replies.