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

标签 php unit-testing mocking phpunit

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

$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/

相关文章:

php - 检查 sessionid 已知的 PHP session 是否处于事件状态

java - 验证是否调用了所有 getter 方法

Python:如何模拟 SQLAlchemy 事件处理程序(使用 mock.unittest)

javascript - 如何获取前一天(连续)

php - MySQL使用LIKE命令在分号之间查找

php - 检测由于违反唯一约束而导致的 mysql 更新/插入失败

java - 为什么使用 JUnit 进行测试?

c# - 我如何访问 asp.net 5 中的内部结构

javascript - 开 Jest : How should I mock a class function?

python - 在 Python 中使用空模式的推荐方法是什么?