PHPUnit: assertInstanceOf() not working

点点圈 提交于 2019-12-03 06:28:08

问题


I need to check if a variable is an object of the User type. User is my class $user my object

$this->assertInstanceOf($user,User);

This is not working, I have a use of undefined constant User - assumed 'User'

Thanks in advance for your help


回答1:


http://apigen.juzna.cz/doc/sebastianbergmann/phpunit/function-assertInstanceOf.html

I think you are using this function wrong. Try:

$this->assertInstanceOf('User', $user);



回答2:


It's always a good idea to use ::class wherever you can. If you get used to this standard, you don't have to use FQCNs (fully qualified classnames), or escape backslashes. Also, IDEs provide better functionality if they know that User here is not just a string, but rather a class.

$this->assertInstanceOf(User::class, $user);



回答3:


Or You can use something like:

$this->assertInstanceOf(get_class($expectedObject), $user);

I usually use this when I'm checking i.e. if setter method is returning reference to self.

$testedObj = new ObjectToTest();
$this->assertInstanceOf(
    get_class($testedObj), 
    $testedObj->setSomething('someValue'),
    'Setter is not returning $this reference'
);


来源:https://stackoverflow.com/questions/16833923/phpunit-assertinstanceof-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!