Difference between assertEquals and assertSame in phpunit?

PHPUnit contains the assertEquals method: https://phpunit.de/manual/current/en/appendixes.assertions.html#appendixes.assertions.assertEquals

It also has an assertSame method: https://phpunit.de/manual/current/en/appendixes.assertions.html#appendixes.assertions.assertSame

At first glance, it looks like they are doing the same thing. What is the difference between the two? Why are they both listed?

+79
phpunit
Apr 20 2018-12-12T00:
source share
6 answers

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 .

And as you can see in the example of the above passage, they pass '2204' and 2204 , which will not work with assertSame , because one of them is string and one is int, basically:

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

assertEquals

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

assertEquals doesn't seem to take into account the data type, so using the above example 2204 :

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

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

+125
Apr 20 '12 at 22:12
source share
 $this->assertEquals(3, true); $this->assertSame(3, true); 

The first will pass!

The second will not be executed.

That is the difference.

I think you should always use assertSame.

+14
Jan 26 '13 at 10:44
source share

When it comes to comparing objects:

assertSame: can only claim if 2 objects refer to the same instance of the object. Therefore, even if 2 separate objects have exactly the same values ​​for all their attributes, assertSame will fail if they do not refer to 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: may claim that in any case 2 separate objects correspond to their attribute values. Thus, it is a method suitable for asserting conformance of an object.

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

https://phpunit.de/manual/current/en/appendixes.assertions.html

+10
Jun 06 '16 at 9:14
source share

Besides,

 // Passes $this->assertSame("123.", "123."); $this->assertEquals("123.", "123"); // Fails $this->assertSame("123.", "123"); 
+1
Feb 13 '15 at 15:04
source share

As already mentioned, AssertSame reports an error if the two elements do not share the type and value , but it is also important to note this from the docummentation :

Reports an error identified by $ message if two $ variables are expected and $ actual does not refer to the same object.

Thus, this test will fail even if they share the type and value:

 class SameTest extends TestCase { public function testFailure() { $this->assertSame(new stdClass, new stdClass); } } 
+1
Nov 23 '16 at 13:26
source share

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

i.e:

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

or

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

assertEquals == If we see in relation to the website page, I have a page with 2 "tables", so when I run assertEquals, I will check its quantity, that the "table" is 2 using the count function. For example:

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

Here we see that assertEquals checks for two tables on a web page. we can also use the partitions found on the page using the "section name" inside the brackets.

For example, 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
Aug 14 '13 at 12:03
source share



All Articles