Replies: 1 comment 2 replies
-
Contain's behavior is weird and inconsistent with the other should operators. Describe 'Contain' {
#? Should [[-ActualValue] <Object>] [-Not] [-ExpectedValue <Object>] [-Because <Object>] [-Contain] [<CommonParameters>]
# Asserts that collection contains a specific value. Uses PowerShell's -contains operator to confirm.
It 'Piped syntax' {
1 | Should -Contain 1
@(1, 2, 3) | Should -Contain 1
@(1, 2, 3) | Should -Contain '1'
'hello', 'world' | Should -Contain 'hello'
'hello', 'world' | Should -Contain 'Hello'
@() | Should -Not -Contain 1
@() | Should -Contain $null
}
It 'Direct syntax' {
Should -ActualValue 1 -Contain 1
{ Should -ActualValue @(1, 2, 3) -Contain 1 } | Should -Throw -ExpectedMessage '*Expected 1 to be found in collection @(@(1, 2, 3)), but it was not found.*'
{ Should -ActualValue @(1, 2, 3) -Contain '1' } | Should -Throw -ExpectedMessage "*Expected '1' to be found in collection @(@(1, 2, 3)), but it was not found.*"
{ Should -ActualValue @('hello', 'world') -Contain 'hello' } | Should -Throw -ExpectedMessage "*Expected 'hello' to be found in collection @(@('hello', 'world')), but it was not found.*"
{ Should -ActualValue @('hello', 'world') -Contain 'Hello' } | Should -Throw -ExpectedMessage "*Expected 'Hello' to be found in collection @(@('hello', 'world')), but it was not found.*"
Should -ActualValue @() -Not -Contain 1
{ Should -ActualValue @() -Contain $null } | Should -Throw -ExpectedMessage '*Expected $null to be found in collection @(@()), but it was not found.*'
}
It 'Single element arrays are evaluated as the single element' {
@(1) | Should -Contain 1
{ Should -ActualValue @(1) -Contain 1 } | Should -Throw -ExpectedMessage '*Expected 1 to be found in collection @(1), but it was not found.*'
}
It 'Arrays are evaluated as a whole when piped to Contain' {
{ @(@(1, 2, 3), @(1, 2, 3)) | Should -Contain 1 } | Should -Throw -ExpectedMessage '*Expected 1 to be found in collection @(@(1, 2, 3), @(1, 2, 3)), but it was not found.*'
{ @(@(1, 2, 3), @(1, 2, 3)) | Should -Contain @(1, 2, 3) } | Should -Throw -ExpectedMessage '*Expected @(1, 2, 3) to be found in collection @(@(1, 2, 3), @(1, 2, 3)), but it was not found.*'
$obj = @(1, 2, 3)
@($obj, @(4, 5, 6)) | Should -Contain $obj
}
It 'Arrays are wrapped with another array when not piped' {
{ Should -ActualValue @(@(1, 2, 3), @(1, 2, 3)) -Contain 1 } | Should -Throw -ExpectedMessage '*Expected 1 to be found in collection @(@(@(1, 2, 3), @(1, 2, 3))), but it was not found.*'
{ Should -ActualValue @(@(1, 2, 3), @(1, 2, 3)) -Contain @(1, 2, 3) } | Should -Throw -ExpectedMessage '*Expected @(1, 2, 3) to be found in collection @(@(@(1, 2, 3), @(1, 2, 3))), but it was not found.*'
$obj = @(1, 2, 3)
{ Should -ActualValue @($obj, @(4, 5, 6)) -Contain $obj } | Should -Throw -ExpectedMessage '*Expected @(1, 2, 3) to be found in collection @(@(@(1, 2, 3), @(4, 5, 6))), but it was not found.*'
Should -ActualValue $obj -Contain $obj
}
It "Omitting '-ActualValue' inputs `$null to the Should command and defaults to '-ExpectedValue'" {
{ Should 1 -Contain 1 } | Should -Throw -ExpectedMessage '*Cannot retrieve the dynamic parameters for the cmdlet. Legacy Should syntax (without dashes) is not supported in Pester 5.*'
{ Should @(1) -Contain 1 } | Should -Throw -ExpectedMessage '*Expected 1 to be found in collection @($null), because 1, but it was not found.*'
}
} |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
When
@()
is piped it passes$null
to contains, which makes sense but unintuitive to me.other than
Should @() -Contain $null
which solves the issue, is it possible to use pipes and avoid this?Beta Was this translation helpful? Give feedback.
All reactions