在我的测试类的设置中,我为用户创建了一个模拟对象。创建模拟时,它会执行如下操作:
$other = $this->getMock( 'Other' );
$user->expects( $this->any() )
->method( '_getOtherInstance' )
->will( $this->returnValue( $other ) );
现在在删除用户时调用_getOtherInstance
删除三级信息。
当我在测试类的tearDown
中运行删除时,在parent::tearDown
之前,_getOtherInstance
返回null
.
我知道模拟已正确设置,因为在 setUp
中运行 delete
可以正常工作。
这里的 tearDown 有什么特别之处?我想 PHPUnit 会取消设置所有模拟及其返回的内容,但在我用 parent::tearDown
调用链之前不会。
最佳答案
setUp
和 tearDown
在 TestCase
中都是空的,您不需要费心从测试中调用它们。它们是供您单独使用的模板方法。这同样适用于静态版本。
调用上面的方法和你的测试方法是runBare
,它调用verifyMockObjects
before调用tearDown
。此方法在每个模拟上调用 __phpunit_verify
,后者依次验证预期,然后删除它们:
public function __phpunit_verify() {
$this->__phpunit_getInvocationMocker()->verify();
$this->__phpunit_invocationMocker = NULL;
}
在 tearDown
中转储模拟对象表明包含期望的调用模拟器已设置为 null
,使它们对您的 tearDown
不可用> 方法。
Danger: If you really must get at the expectation, you can override
verifyMockObjects
in your test case since it's a protected method. Grab what you need and then call the parent method. Just keep in mind that you're treading in the PHPUnit internals which change quite frequently.
关于php - 为什么模拟的 returnValue 在 phpunit 的拆卸中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25211132/