此测试由Sebastian Bergmann在Silverstripe 3.4.0上使用PHPUnit 3.7.38运行
OverrideTest.yml
ExtMember:
ext_member:
Email: ext@email.com
Password: extpassword
的 OverrideTest.php
class ExtMember extends Member {}
class OverrideTest extends SapphireTest
{
protected static $fixture_file = 'OverrideTest.yml';
public function testBrokenFixtures()
{
$MockExtMember = $this->getMockBuilder('ExtMember')->getMock();
$extMember = $this->objFromFixture("ExtMember", "ext_member");
Injector::inst()->registerService($MockExtMember, 'ExtMember');
$extMemberNull = $this->objFromFixture("ExtMember", "ext_member");
$this->assertEquals("ext@email.com", $extMember->Email);
$this->assertNull($extMemberNull->Email);
}
}
$extMemberNull
对象发生了什么变化?为什么在将DataObject注册为服务后,所有get调用都返回null?就可测试性而言,这是一个巨大的障碍
答案 0 :(得分:1)
当您访问DataObject($extMember->Email
)上的属性时,它由magic方法__get()处理并导致调用:
由于您尝试在没有配置的情况下使用模拟对象,因此这些方法返回NULL