2013-08-01 24 views
1

我按照此頁面上的說明,但無法讓我的單元測試工作。故障運行單元測試 - 得到未定義的方法錯誤

http://framework.zend.com/manual/2.2/en/tutorials/unittesting.html

我最初的代碼是這樣的:

<?php 

namespace ApplicationTest\Controller; 

use Zend\Http\Request; 
use Zend\Http\Response; 
use Zend\Test\PHPUnit\Controller\AbstractHttpControllerTestCase; 

class IndexControllerTest extends AbstractHttpControllerTestCase { 

    protected $controller; 
    protected $request; 
    protected $response; 
    protected $routeMatch; 
    protected $event; 
    protected $traceError = true; 

    public function setUp() { 

     $this->setApplicationConfig(
      include '../../../config/application.config.php' 
     ); 
     parent::setUp(); 
    } 

    public function testIndexActionCanBeAccessed() { 

     $this->dispatch('/'); 
     $this->assertResponseStatusCode(200); 

    } 
} 

當我跑PHPUnit的,我得到了以下錯誤消息:

PHPUnit的21年7月3日由塞巴斯蒂安·伯格曼。

配置從/usr/share/php/tool/module/Application/test/phpunit.xml

onDispatch調用。 Ë

時間:1秒,內存:14.50Mb

有1錯誤:

1)ApplicationTest \控制器\ IndexControllerTest :: testIndexActionCanBeAccessed 的Zend \的ServiceManager \異常\ ServiceNotFoundException的:Zend的\的ServiceManager \ ServiceManager :: get無法爲Zend \ Db \ Adapter \ Adapter提取或創建一個實例

然後我按照第二組指令來配置服務管理器。塞巴斯蒂安伯格曼

PHPUnit的21年7月3日:

public function testIndexActionCanBeAccessed() { 

    $albumTableMock = $this->getMockBuilder('User\Model\UserData') 
     ->disableOriginalConstructor() 
     ->getMock(); 

    $albumTableMock->expects($this->once()) 
     ->method('getUserSessionArray') 
     ->will($this->returnValue(array())); 

    $serviceManager = $this->getApplicationServiceLocator(); 
    $serviceManager->setAllowOverride(true); 
    $serviceManager->setService('User\Model\UserData', $albumTableMock); 

    $this->dispatch('/'); 
    $this->assertResponseStatusCode(200); 

} 

而這個時候,我得到了下面的錯誤。

配置從/usr/share/php/tool/module/Application/test/phpunit.xml

onDispatch調用。 PHP致命錯誤:調用未定義的方法Mock_UserData_ae821217 :: getUserSessionArray()在/usr/share/php/tool/module/User/Module.php上線95 PHP堆棧跟蹤: PHP 1. {main}()/ usr/local/pear/bin/phpunit:0 ...

有人可以幫我解決這個問題嗎?

我們使用的是Zend Framework 2.2.0。

非常感謝。

EC

回答

3

您的模擬設置不正確。你不設置任何模擬方法,所以你的期望沒有被設置。您需要創建您的模擬像這樣:

$albumTableMock = $this->getMockBuilder('User\Model\UserData') 
    ->disableOriginalConstructor() 
    ->setMethods(array('getUserSessionArray')) //ADD this line 
    ->getMock(); 

User\Model\UserData類不存在等的PHPUnit沒有創造得到嘲笑的方法。當你運行你的測試時,函數沒有被定義。

+0

謝謝。然而,在ZF2文檔中,另一個明顯重要的代碼被排除和/或忽略或遺忘。 – dKen

相關問題