gpt4 book ai didi

php - 为什么模拟的 returnValue 在 phpunit 的拆卸中不起作用?

转载 作者:可可西里 更新时间:2023-10-31 23:47:53 26 4
gpt4 key购买 nike

在我的测试类的设置中,我为用户创建了一个模拟对象。创建模拟时,它会执行如下操作:

$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 调用链之前不会。

最佳答案

setUptearDownTestCase 中都是空的,您不需要费心从测试中调用它们。它们是供您单独使用的模板方法。这同样适用于静态版本。

调用上面的方法和你的测试方法是runBare,它调用verifyMockObjectsbefore调用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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com