Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么mocked returnValue在撕毁phpunit时不起作用?_Php_Unit Testing_Mocking_Phpunit - Fatal编程技术网

为什么mocked returnValue在撕毁phpunit时不起作用?

为什么mocked returnValue在撕毁phpunit时不起作用?,php,unit-testing,mocking,phpunit,Php,Unit Testing,Mocking,Phpunit,在测试类的设置中,我为用户创建了一个模拟对象。创建模拟时,它会执行以下操作: $other = $this->getMock( 'Other' ); $user->expects( $this->any() ) ->method( '_getOtherInstance' ) ->will( $this->returnValue( $other ) ); 现在在删除用户时,它调用\u getOtherInstance删除第三方信息 当我在

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

$other = $this->getMock( 'Other' );

$user->expects( $this->any() )
     ->method( '_getOtherInstance' )
     ->will( $this->returnValue( $other ) );
现在在删除用户时,它调用
\u getOtherInstance
删除第三方信息

当我在测试类“
tearDown
中运行delete时,在
parent::tearDown
之前,
\u getOtherInstance
返回
null

我知道模拟设置正确,因为在
setup
中运行
delete


这里拆卸有什么特别之处?我可以想象PHPUnit会取消所有mock和它们返回的内容,但在我调用
parent::tearDown

链之前,
setUp
tearDown
TestCase
中都是空的,您不需要从测试中调用它们。它们是供您单独使用的模板方法。这同样适用于静态版本

调用上述和您的测试方法的方法是
runBare
,它在调用
tearDown
之前调用
verifyMockObjects
。此方法对每个模拟调用
\uuuuu phpunit\u verify
,然后依次验证期望值并删除它们:

tearDown
中转储mock对象表明,包含期望值的调用mock已设置为
null
,使它们对
tearDown
方法不可用

危险:如果您真的必须达到预期目标,您可以在测试用例中重写
verifyMockObjects
,因为它是一种受保护的方法。获取所需内容,然后调用父方法。请记住,您正处在PHPUnit内部,它经常发生变化

您应该提供
设置
中的代码(或示例代码)、测试本身和
拆卸
。你可能根本不应该做任何拆卸。
public function __phpunit_verify() {
    $this->__phpunit_getInvocationMocker()->verify();
    $this->__phpunit_invocationMocker = NULL;
}