简单的 Powershell Pester 测试不适用于 $false
Simple Powershell Pester Test not working for $false
我编写了一个名为 "Check-RegValueExists" 的非常简单的函数,当直接从命令提示符执行时,它的工作原理如下
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"
输出如下
True
False
False
这些都是正确的结果。
我的纠缠测试是
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
}
}
但是只有 第一个测试通过,其他两个测试失败,随后
[-] 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
我什至复制并粘贴了第一个测试,只修改了字符串来确认,但仍然失败。是否存在语法错误或其他问题?
您的问题是因为您将命令用单引号引起来,这将它们变成了字符串。因此,您正在测试每个字符串是 $true 还是 $false 并且任何值都被视为 $true.
您需要这样做:
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
}
}
我编写了一个名为 "Check-RegValueExists" 的非常简单的函数,当直接从命令提示符执行时,它的工作原理如下
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"
输出如下
True
False
False
这些都是正确的结果。
我的纠缠测试是
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
}
}
但是只有 第一个测试通过,其他两个测试失败,随后
[-] 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
我什至复制并粘贴了第一个测试,只修改了字符串来确认,但仍然失败。是否存在语法错误或其他问题?
您的问题是因为您将命令用单引号引起来,这将它们变成了字符串。因此,您正在测试每个字符串是 $true 还是 $false 并且任何值都被视为 $true.
您需要这样做:
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
}
}