Difference between assertEquals and assertSame in PHPUnit?

后端 未结 7 1148
别那么骄傲
别那么骄傲 2021-01-30 18:54

PHPUnit contains an assertEquals() method, but it also has an assertSame() one. At first glance it looks like they do the same thing.

What is the difference between the t

7条回答
  •  执笔经年
    2021-01-30 19:34

    I use both sporadically, but according to the docs:

    assertSame

    Reports an error identified by $message if the two variables $expected and $actual do not have the same type and value."

    And as you can see in the example below the above excerpt, they are passing '2204' and 2204, which will fail using assertSame because one is a string and one is an int, basically:

    '2204' !== 2204
    assertSame('2204', 2204) // this test fails
    

    assertEquals

    "Reports an error identified by $message if the two variables $expected and $actual are not equal."

    assertEquals does not appear to take datatype into consideration so using the above example of 2204:

    '2204' == 2204
    assertEquals('2204', 2204) // this test passes
    

    I just ran some unit tests against the above examples, and indeed they resulted in documented behavior.

提交回复
热议问题