-
-
Notifications
You must be signed in to change notification settings - Fork 473
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Should-BeNull
: Strange behaviour when $null
#2555
Comments
If I actually return BeforeAll {
function Out-Something
{
param
(
)
$null
}
}
Describe 'Out-Something' {
It 'Should not output anything' {
$result = $null
$result = Out-Something
Write-Verbose ("Result is null: {0}" -f ($null -eq $result)) -Verbose
Write-Verbose ("Result is array: {0}" -f ($result -is [array])) -Verbose
$result | Should-BeNull
}
} Returns:
|
For some reason This doesn't seem to be a Pester issue, but probably how PowerShell works... I just don't understand why it behaves as it does. If someone could shed som light it would be appreciated. 🙂 |
Those are artifacts of using the pipeline, when you use pipeline we collect it by it is empty When it is empty there was no input, we check for being called via pipeline (not in the function below, but in the real Collect-Input function in Pester), so the only case when that happens is when you get called with empty collection e.g. When you pass When it has more items it there are no edge cases, it is simply a collection. function empty () { }
function returnNull () { return $null }
function check () {
param(
[Parameter(Position = 1, ValueFromPipeline = $true)]
$actual
)
"is null: $($null -eq $local:input)"
"has type: $($local:input.GetType().Name)"
"count: $($local:input.Count)"
}
$null | check
"--- empty function:"
empty | check
"--- empty array:"
@() | check
"--- null:"
$null | check
"--- return null:"
returnNull | check
"--- item:"
1 | check
"--- item in array:"
@(1) | check
PowerShell also behaves differently when passing values in pipeline and when assigning to a variable. Which is how you are debugging this and why this is giving you different results: function arr () { @() };
$e = arr
"is `$e null: $($null -eq $e)"
# BUT here we don't receive `$null` we receive empty
arr | check I agree that this is unexpected, but is is how powershell binds the methods via pipeline and in assignments. You will also observe similar difference when using the parameter syntax of
|
Now that is the technical reasoning, but how do we make it usable in practice? |
Checklist
What is the issue?
I'm getting that the
$result
passed toShould-BeNull
is not$null
, but the verbose output I added to debug this do say it is not an array and it is indeed$null
. 🤔 Not sure if I'm doing something wrong. I have seen this in another place as well but ignored it there, when I got it here I thought this must be an error somewhere. 🙂The
Out-Diff
currently only writesWrite-Information
messages and returns no values.I output verbose message to determine that it is indeed $null and not an array, still
Should-BeNull
thinks it is.Expected Behavior
Should pass test as the actual value is $null.
Steps To Reproduce
Running this reproduces the issue:
Returns:
Describe your environment
Possible Solution?
Hopefully I'm just doing something wrong. 🙂
The text was updated successfully, but these errors were encountered: