【发布时间】:2017-05-09 01:16:58
【问题描述】:
我想模拟一个类来测试它。它有一个由网络请求设置的private property detail。我试图通过模拟json file 在测试其他methods 之前设置property。一切正常,除了,当它是私有属性时我似乎无法设置该属性,但当它是受保护的属性时可以工作。
$mockedClass = \Mockery::mock( Myclass::class )->makePartial();
$reflection = new \ReflectionClass($mockedClass);
$property = $reflection->getProperty( 'detail' );
$property->setAccessible(true);
$property->setValue($mockedClass, $jsonData );
所以当 detail 是 private property 时,它 Mockery 会抛出 Property detail does not exist,但是当我将 detail 设置为受保护时,它可以工作。
我不想让detail 成为protected property,因为它不需要,但我需要这样做来测试它。
当我在某处读到“就像你妈妈说的那样,不要暴露你的私处”。我不想暴露我的隐私。
【问题讨论】:
-
你在“'detail”之后错过了单个配额
-
@RaghavRangani 我的错,修复它。
-
还是你的问题??
-
@RaghavRangani 这是我的问题,我不会收到我提到的其他错误,我只会收到缺少引号的错误。
标签: php unit-testing testing phpunit mockery