phpunit中的assertEquals和assertSame之间的区别? [英] Difference between assertEquals and assertSame in phpunit?
问题描述
PHPUnit包含一个assertEquals方法: https://phpunit.de/manual/current/en/appendixes.assertions.html#appendixes.assertions.assertEquals
PHPUnit contains an assertEquals method: 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
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 do the same thing. What is the difference between the two? Why are they both specified?
推荐答案
我偶尔使用两者,但根据文档:
I use both sporadically, but according to the docs:
如果两个变量
$expected
和$actual
具有不同的 type 和 value ,则报告由$message
标识的错误."
Reports an error identified by
$message
if the two variables$expected
and$actual
do not have the same type and value."
并且如您在上面摘录下面的示例中所见,它们正在传递'2204'
和2204
,它们将使用assertSame
失败,因为一个基本上是string
而另一个是int,
:
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
如果两个变量$ expected和$ actual不相等,则报告由$ message标识的错误."
"Reports an error identified by $message if the two variables $expected and $actual are not equal."
assertEquals
似乎没有考虑数据类型,因此使用上面的2204
示例:
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.
这篇关于phpunit中的assertEquals和assertSame之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!