2013-08-22 28 views
2

我用TYPO3 6.1測試功能。Extbase:與注射庫

其實我嘗試使用PHPUnit測試方法,該方法使用注射庫。

$mock = $this->getMockedRepository(
    '\\MyExt\\Domain\\Repository\\MyRepository', 
    array(
     'findByUid' => array('count' => 0, 'return' => array()), 
    ) 
); 
$this->tokenHelperObj->injectMyRepository($mock); 

功能getMockedRepository是剛剛從PHPUnit的調用getMock - 函數一個幫手。在其他情況下,該功能起作用。在我的幫助類,我只是使用依賴注入像

/** 
* myRepository 
* 
* @var \MyExt\Domain\Repository\MyRepository 
* @inject 
*/ 
protected $myRepository; 

當我打電話的測試,我得到

Call to undefined method Class::injectActivityRepository() 

我不想寫的不同的存儲庫我使用所有的注射的方法。有沒有其他方法來模擬注入的存儲庫?

+0

什麼是'$這個 - > tokenHelperObj'以及你如何讓存儲庫中有你的擴展代碼(不是測試)? – Michael

+0

'$這個 - > tokenHelperObj'是它在初始化初始化我的燈具類,我想測試。我用'@ inject'注入我的庫,就像寫在問題中一樣。 – freshp

回答

4

由於TYPO3 6.1 \ TYPO3 \ CMS \核心\測試\ UnitTestCase已擴展的新方法被稱爲注入類()。這個方法可以用來注入依賴,所以你不必創建注入方法。

用法:

$this->inject($target, $name, $dependency) 

下面如下的例子測試:

/** 
* @test 
*/ 
public function serviceReturnsFalseIfNoRecordsFoundTest() { 
    $mockQuery = $this->getMock('TYPO3\CMS\Extbase\Persistence\QueryInterface'); 

    $mockRepository = $this->getMock('\TYPO3\MyExtension\Domain\Repository\TestRepository'); 
    $mockRepository->expects($this->once())->method('findAll')->will($this->returnValue($mockQuery)); 

    $this->inject($this->fixture, 'testRepository', $mockRepository); 

    $this->assertTrue($this->fixture->doSomething()); 
} 
+0

您好,$ this-> inject($ target,$ name,$ dependency)方法位於\ TYPO3 \ CMS \ Extbase \ Tests \ Unit \ BaseTestCase中。不在「\ TYPO3 \ CMS \ Core \ Tests \ UnitTestCase」中! –

+0

'$ this-> fixture'看起來像什麼?我在這裏有點難住.. – pduersteler

+0

'$ this-> fixture'是你正在進行單元測試的類的主要實例。通常你在測試類的'setup()'中創建它。有關示例,請參閱https://github.com/derhansen/sf_event_mgt/blob/master/Tests/Unit/Service/ExportServiceTest.php(請注意,我的變量被稱爲'$ subject'而不是'$ fixture') – derhansen