2

I wrote a very simple function called "Check-RegValueExists" which works as follows when executing directly from the command prompt

Check-RegValueExists "HKLM:\Software\Microsoft\Windows\CurrentVersion" "DevicePath"

Check-RegValueExists "HKLM:\Software\Microsoft\Windows\CurrentVersion" "Devicexxxx"

Check-RegValueExists "HKCU:\Software\Classes\message-facebook-com" "URL Protocol"

which outputs the following

True
False
False

which are all correct outcomes.

My pester test is

Describe 'Check-RegValueExists Tests'{

It "has valid key with valid value" {
        'Check-RegValueExists "HKLM:\Software\Microsoft\Windows\CurrentVersion" "DevicePath"' | Should Be $true
        } 

It "has invalid key" {
        'Check-RegValueExists "HKLM:\Software\Microsoft\Windows\CurrentVersion" "Devicexxxx"' | Should Be $false
        } 

It "has empty value" {
        'Check-RegValueExists "HKCU:\Software\Classes\message-facebook-com" "URL Protocol"' | Should Be $false
        } 

}

However only the first test passes and other two fail with following

[-] has invalid key 17ms
   Expected: {False}
   But was:  {Check-RegValueExists "HKLM:\Software\Microsoft\Windows\CurrentVersion" "Devicexxxx"}
   8:         'Check-RegValueExists "HKLM:\Software\Microsoft\Windows\CurrentVersion" "Devicexxxx"' | Should Be $false
   at <ScriptBlock>, <No file>: line 8
 [-] has empty value 28ms
   Expected: {False}
   But was:  {Check-RegValueExists "HKCU:\Software\Classes\message-facebook-com" "URL Protocol"}
   12:         'Check-RegValueExists "HKCU:\Software\Classes\message-facebook-com" "URL Protocol"' | Should Be $false
   at <ScriptBlock>, <No file>: line 12

I even copied and pasted first test and modified only the string to confirm but still failing. Is there a syntax error or something else?

Mark Wragg
  • 22,105
  • 7
  • 39
  • 68
  • if I change the assertion to `Should be $true` then the tests pass!!! which does not make logical sense? is $true simply testing for any value to appear or successful running of the function??? – user2743945 Nov 01 '17 at 19:37
  • 1
    Per my answer, your main issue is you have wrapped your commands in quotes turning them in to strings. However it would help if you also added your `Check-RegValueExists` function to your question via an edit so that we can see what it does. You should make sure that it returns $true or $false and not the strings "true" or "false". – Mark Wragg Nov 01 '17 at 20:16

1 Answers1

3

Your issue is because you have wrapped your commands in single quotes, which has turned them in to strings. As a result you are testing whether each string is $true or $false and any value is considered $true.

You need to do this:

Describe 'Check-RegValueExists Tests'{

    It "has valid key with valid value" {
        Check-RegValueExists "HKLM:\Software\Microsoft\Windows\CurrentVersion" "DevicePath" | Should Be $true
    } 

    It "has invalid key" {
        Check-RegValueExists "HKLM:\Software\Microsoft\Windows\CurrentVersion" "Devicexxxx" | Should Be $false
    } 

    It "has empty value" {
        Check-RegValueExists "HKCU:\Software\Classes\message-facebook-com" "URL Protocol" | Should Be $false
    } 
}
Mark Wragg
  • 22,105
  • 7
  • 39
  • 68
  • OMG, wow, how thick could I be, your 100% right, thank you. I was following a tutorial and in that they seemed to be doing everything in quotes and then when i tried something for myself I just followed the convention. Man I feel stupid. – user2743945 Nov 03 '17 at 01:56
  • 2
    Also, you could use `| Should -BeTrue` or `| Should -BeFalse` as well. – fourpastmidnight Nov 02 '18 at 14:36