assertEquals和assertSame在phpunit之间的区别?
PHPUnit包含一个assertEquals方法: https : //phpunit.de/manual/current/en/appendixes.assertions.html#appendixes.assertions.assertEquals
它也有一个assertSame方法: https ://phpunit.de/manual/current/en/appendixes.assertions.html#appendixes.assertions.assertSame
乍一看,它们看起来像是做同样的事情。 两者有什么区别? 为什么他们都指定?
我偶尔使用,但根据文档:
 assertSame 
如果两个variables
$expected和$actual不具有相同的types和值,则报告由$message标识的错误。
 正如你在上面摘录的例子中看到的那样,它们传递'2204'和2204 ,这将会使用assertSame失败,因为一个是string ,一个是int,基本上是: 
 '2204' !== 2204 assertSame('2204', 2204) // this test fails 
 assertEquals 
“如果两个variables$ expected和$ actual不相等,则报告由$ message标识的错误。”
  assertEquals似乎没有考虑数据types,所以使用上面的2204例子: 
 '2204' == 2204 assertEquals('2204', 2204) // this test passes 
我只是对上面的例子进行了一些unit testing,事实上他们导致了文档化的行为。
 $this->assertEquals(3, true); $this->assertSame(3, true); 
第一个会通过!
第二个将失败。
这是不同的。
我想你应该总是使用assertSame。
说到对象比较:
assertSame:只能在2个对象引用相同的对象实例时才能断言。 因此,即使2个独立的对象的所有属性都具有完全相同的值,但如果不引用相同的实例,assertSame将会失败。
  $expected = new \stdClass(); $expected->foo = 'foo'; $expected->bar = 'bar'; $actual = new \stdClass(); $actual->foo = 'foo'; $actual->bar = 'bar'; $this->assertSame($expected, $actual); FAILS 
assertEquals:如果在任何情况下2个独立的对象匹配它们的属性值,可以断言。 所以这是适合断言对象匹配的方法。
  $this->assertEquals($expected, $actual); PASSES 
https://phpunit.de/manual/current/en/appendixes.assertions.html
此外,
 // Passes $this->assertSame("123.", "123."); $this->assertEquals("123.", "123"); // Fails $this->assertSame("123.", "123"); 
 如前所述,如果两个元素不共享types和值 ,则AssertSame报告错误,但是从文档中注意到这一点也很重要: 
如果两个variables$ expected和$ actual不引用同一个对象,则报告由$ message标识的错误。
所以这个testing也会失败,即使他们共享types和价值:
 class SameTest extends TestCase { public function testFailure() { $this->assertSame(new stdClass, new stdClass); } } 
assertSame()==testing如果实际的输出和期望的参数是相同的。
那是 :
 $this->assertSame('$expected','$expected'); 
要么
 $this->assertSame('100','100'); 
assertEquals ==如果我们看到一个网站页面,我有一个页面有2'表',所以当我运行assertEquals我会检查它的计数,'表'是2通过使用计数函数。 例如:
 $this->assertEquals(2, $var->filter('table')->count()); 
在这里我们可以看到,assertEquals检查网页上有两个表格。 我们也可以使用括号内的“#division name”在页面上使用分区。
例如2:
 public function testAdd() { $calc = new Calculator(); $result = $calc->add(30, 12); // assert that our calculator added the numbers correctly! $this->assertEquals(42, $result); }