Difference between assertEquals and assertSame in PHPUnit?

后端 未结 7 1151
别那么骄傲
别那么骄傲 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条回答
  •  -上瘾入骨i
    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());
        }
    }
    

提交回复
热议问题