Difference between assertEquals and assertSame in PHPUnit?

后端 未结 7 1144
别那么骄傲
别那么骄傲 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.

    0 讨论(0)
  • 2021-01-30 19:34

    assertSame() == Tests that if the actual output and the expected parameter are same.

    that is :

    $this->assertSame('$expected','$expected');
    

    or

    $this->assertSame('100','100');
    

    assertEquals == If we see with respect to a website page, i have a page which has 2 'table' so when i run assertEquals i will check its count that the 'table' are 2 by using a count function. Eg:

    $this->assertEquals(2, $var->filter('table')->count()); 
    

    Here we can see that assertEquals checks that there are 2 tables found on the web page. we can also use divisions found on the page using '#division name' inside the bracket.

    Eg 2:

    public function testAdd()
    {
        $calc = new Calculator();
    
        $result = $calc->add(30, 12);
    
        // assert that our calculator added the numbers correctly!
        $this->assertEquals(42, $result);
    }
    
    0 讨论(0)
  • 2021-01-30 19:43

    Moreover,

    // Passes
    $this->assertSame("123.", "123.");
    $this->assertEquals("123.", "123");
    // Fails
    $this->assertSame("123.", "123");
    
    0 讨论(0)
  • 2021-01-30 19:52
    $this->assertEquals(3, true);
    $this->assertSame(3, true);
    

    The first one will pass!

    The second one will fail.

    That is the difference.

    I think you should always use assertSame.

    0 讨论(0)
  • 2021-01-30 19:53

    When it comes to objects comparison:

    assertSame

    Can only assert if two objects are referencing the same object instance. So even if two separate objects have for all of their attributes exactly the same values, assertSame() will fail if they don't reference the same instance.

    $expected = new \stdClass();
    $expected->foo = 'foo';
    $expected->bar = 'bar';
    
    $actual = new \stdClass();
    $actual->foo = 'foo';
    $actual->bar = 'bar';
    
    $this->assertSame($expected, $actual); // FAILS
    

    assertEquals

    Can assert if two separate objects match their attribute values in any case. So it's the method suitable for asserting object match.

    $this->assertEquals($expected, $actual); // PASSES
    

    Reference

    0 讨论(0)
  • 2021-01-30 19:57

    As previously mentioned, assertEquals() is primarily about an interpreted value, be it by type juggling or an object with an __magic presentation method (__toString() for example).

    A good use case for assertSame() is testing a singleton factory.

    class CacheFactoryTest extends TestCase
    {
        public function testThatCacheFactoryReturnsSingletons()
        {
            $this->assertSame(CacheFactory::create(), CacheFactory::create());
        }
    }
    
    0 讨论(0)
提交回复
热议问题