Skip to content

Handling different types in Shoulds #703

@nohwnd

Description

@nohwnd

As highlighted by this issue, there are some changes in the way Should Be works. At the current point the $Actual side of the assertion determines the type to be used to do the equality check, and in v3 the $Expected side determines the type to be used.

This leads to subtle bugs where the expected value is upgraded to a reference type and compared by reference instead of by value.

Here the expected string value is upgraded to script block:

describe "a" { 
    it "b" { 
        {hello} | should be "hello"
    }
}

# [-] b 29ms
#   Expected: {hello}
#   But was:  {hello}
#   at line: 3 in
#   3:         {hello} | should be "hello"

Here the expected value is upgraded to Boolean (sting False becomes $true boolean):

describe "a" { 
    it "b" { 
        $false | should be False
    }
}

# [-] b 27ms
#   Expected: {False}
#   But was:  {False}
#   at line: 3 in
#   3:         $false | should be False

In v4 this is equivalent to doing $Actual -eq $Expected, where the left side determines the type. The actual code to be modified is here https://github.com/pester/Pester/blob/DevelopmentV4/Functions/Assertions/Be.ps1#L214 (as well as on 210 and possibly other lines).

TODO:
I need to write tests to highlight the issue.
See how it behaves for object types.
Possibly make the exception messages better by mentioning the types.

Go through all assertions and make sure the $expected side is on the left and $actual on the right (in assertions where it makes sense).

Check if the behavior makes sense if there is reference type on the Expected side and value type on the actual side.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions