Php assertAttributeEqual不';不适用于私有属性
我有这本书中的代码,但我无法通过phpunit4.3Php assertAttributeEqual不';不适用于私有属性,php,phpunit,Php,Phpunit,我有这本书中的代码,但我无法通过phpunit4.3 class Foo { protected $message; protected function bar($environment) { $this->message = "PROTECTED BAR"; if($environment == 'dev') { $this->message = 'CANDY BAR'; } }
class Foo {
protected $message;
protected function bar($environment) {
$this->message = "PROTECTED BAR";
if($environment == 'dev') {
$this->message = 'CANDY BAR';
}
}
}
class FooTest extends PHPUnit_Framework_TestCase {
public function testProtectedBar() {
$expectedMessage = 'PROTECTED BAR';
$reflectedFoo = new ReflectionMethod('Foo', 'bar');
$reflectedFoo->setAccessible(true);
$reflectedFoo->invoke(new Foo(), 'production');
$this->assertAttributeEquals(
$expectedMessage,
'message',
$reflectedFoo,
'Did not get expected message'
);
}
}
在运行PHPUnit之后,我得到了
'PHPUnit\u Framework\u Exception:属性“message”未在对象中找到。
这很奇怪,或者可能API在4.3中刚刚更改过?您几乎拥有了它,您必须将一个Foo实例传递给assertAttributeEquals
而不是ReflectionMethod
类的实例
class FooTest extends PHPUnit_Framework_TestCase {
public function testProtectedBar() {
$expectedMessage = 'PROTECTED BAR';
$reflectedFoo = new ReflectionMethod('Foo', 'bar');
$reflectedFoo->setAccessible(true);
$foo = new Foo();
$reflectedFoo->invoke($foo, 'production');
$this->assertAttributeEquals(
$expectedMessage,
'message',
$foo,
'Did not get expected message'
);
}
}
在PHPUnit的官方4.3文档中,我找不到有关assertAttributeEquals
的适当文档,只是对它的引用
assertAttributeEquals()和assertAttributeEquals()非常方便
使用类的公共、受保护或私有属性的包装器
或对象作为实际值
我为它找到了一些drupal,它指定了它的参数列表
assertAttributeEquals
负责修改第二个形式参数($actualAttributeName
)指定的属性的可见性 抱歉,没有,只是复制并粘贴了您的代码,仍然是相同的错误。只是添加了公共属性,错误消息是相同的:“在对象中找不到属性“xxx”