在我的PHPUnit测试中,我想声明我正在测试的类扩展了另一个类。我怎么能用PHPUnit做到这一点?
答案 0 :(得分:17)
使用assertInstanceOf()
代替PHP内置的instanceof
运算符或函数,以便获得有意义的失败消息。
function testInstanceOf() {
$obj = new Foo;
self::assertInstanceOf('Bar', $obj);
}
...
Failed asserting that <Foo> is an instance of class "Bar".
答案 1 :(得分:1)
使用instanceof怎么样?
- &GT; http://php.net/manual/en/internals2.opcodes.instanceof.php
答案 2 :(得分:1)
is_subclass_of()
(或可能is_a()
)可能就是您要找的。 p>
答案 3 :(得分:0)
或者你也应该像这样使用这个断言:
$this->assertSame(
'Symfony\Component\Form\AbstractType',
get_parent_class('AppBundle\Form\CarType'),
'The form does not extend the AbstractType class'
);