1
votes

I have this test:

It 'Should not find environment' {

    {Add-Patchgroup -ComputerName $serversHash.serverWithNotExistingEnvironment -WarningVariable warning -WarningAction SilentlyContinue}
    $warning  | Should -BeLike ('*55555*')
}

$warning contains this string (exactly as you see here with the newline and whitespace on the second row):

Could not add <nameOfTheServer> to patchgroup
 Exception message: Environment F was not found

But my test is passing which it should not:

Context Find the environment
      [+] Should not find environment 79ms
      [+] Should find environment 147ms

It seems like the test is not working at all even with -Be parameter. The test is still passing.

$warning  | Should -Be 'randomrandom'
2

2 Answers

1
votes

I'm not sure why your test is passing, and that is very strange. But I think part of your issue may be that you are executing your command within a scriptblock, and that therefore the warning variable is only populated/accessible within the scope of that scriptblock.

Also, generally I also tend to do my execution outside of the It and then only use the it block for the assertion.

I therefore suggest you try this:

Add-Patchgroup -ComputerName $serversHash.serverWithNotExistingEnvironment -WarningVariable warning -WarningAction SilentlyContinue

It 'Should not find environment' {
    $warning  | Should -BeLike '*55555*'
}

Note I've also removed the brackets around the string you are asserting $warning to be like, as I think these are redundant.

0
votes

Old, not really relevant to Should -BeLike, but still brought here by the googz so here's my explanation for those that follow:

You're not executing anything. You're declaring a script block, automatically outputting it. $warning is still whatever it was before the intended running. If running the file from within a session, or Visual Code, it could very well have been set by previous code.

Check your variable values by debugging.