PHPUNIT Zend 框架如何模拟 Zend_Registry::set 仅针对第一个数据源项进行测试并为第二个数据源项取消设置?

问题描述 投票:0回答:1

我在使用 PHPUNIT 和 Zend 框架创建此 UT 时遇到问题

这就是 UT 的功能:

 protected function _functionToTest( $view ) 
 {
    if($this->domagick) {
        $view->details = array(
                   'important value' => Zend_Registry::get('importantvalue')
                   'Some String' => $this->_getZendHelper()
        );
    }
 }

注意

$this->_getZendHelper()
是引入
Zend_Registry::set('importantValue', '123');

的函数

第二个断言失败,我应该创建 2 个单独的测试吗?

有什么办法可以制作吗

Zend_Registry::set('importantValue', '123');

针对第一个数据提供者(data1)运行 然后在第二次运行时禁用它?因为它应该为 null,并且它正在创建一个数组而不是 null。 或者我应该以某种特定的方式嘲笑它?

我的测试代码:

   /** @dataProvider functionToTestDataProvider */     public function testfunctionToTest($message, $siteMagicSettings, $expected)     { // this is the value that is I can't understand how to "reset" // for the second dataprovider test which expects null         Zend_Registry::set('importantValue', '123');         $myMockForDebug = $this->getMockBuilder('CLASS_Name_Jack')             ->disableOriginalConstructor()             ->setMethods(array('_getZendHelper, _functionToTest'))             ->getMock();         $myMockForDebug = $this->getPublicClass($myMockForDebug);         $myMockForDebug->_siteMagicSettings = $siteMagicSettings;         $myMockForDebug->expects($this->any())             ->method('_getZendHelper')             ->will($this->returnValue('hello world'));         $zendFrameworkControllerFrontEndMockVariable= $this->getMockBuilder('Zend_Controller_Front')             ->disableOriginalConstructor()             ->setMethods(                 array(                     'getMagicalContext';                 )             )             ->getMock();         $myMockForDebug->_zendControllerFront = $zendFrameworkControllerFrontEndMock;         $view = $myMockForDebug->_zendControllerFront;         $myMockForDebug->_functionToTest($view);         $this->assertEquals($expected , $view->details);         $this->assertNull($expected , $view->details);     }

我的数据提供者:

    public function functionToTestDataProvider()     {         return [             'data1'=> [                     'message' => 'Do magick is true so it should create expected',                     'siteMagicSettings' => (object) array(                         'values' => (object) array(                             'doMagick'=> 'true'                         )                     ),                     'expected' => array(                             'important value' => '123',                             'Some String' => 'hello world'                     ),             ],             'data2'=> [                     'message' => 'Do magick is turned off',                     'siteMagicSettings' => (object) array(                         'values' => (object) array(                             'doMagick'=> 'false'                         )                     ),                     'expected' => null             ]         ];     }

目前当我运行测试时,我得到 1 次失败:

"data2" ('Do magick is turned off', stdClass, NULL) Array (...) does not match expected type "NULL".


zend-framework phpunit
1个回答
2
投票
它们已经由框架测试过

这就是我重构你的代码的方式

protected function _functionToTest($carData, $helperString) { return [ 'important value' => $carData 'Some String' => $helperString ]; } // ..and somewhere $helperString = $this->_getZendHelper(); $view->details = $this->domagick ? $this->_functionToTest($this->car, $helperString) : [];

你的函数实际上只是创建一个数组。

© www.soinside.com 2019 - 2024. All rights reserved.