[TYPO3-mvc] Problems with testing Controllers (TDD)

Michael Knoll mimi at kaktusteam.de
Sun Jan 10 02:29:56 CET 2010


Hi,

sorry for not writing back for a longer time... but had to spend some 
time in other projects...

So here is my experience: It works greate. After Jochen's tip, I decided 
to write tests like that:

     /**
      * @test
      */
     public function editActionWorks() {
     	$mockGallery = $this->getMock('Tx_Yag_Domain_Model_Gallery', 
array('getAlbums'), array(), '', FALSE);
 
$mockGallery->expects($this->once())->method('getAlbums')->will($this->returnValue(array('album1', 
'album2')));

         // Dirty trick, as object is cloned when passed to view via 
assign. So make
         // compared object cloned to in order to make assertion working.
         $clonedMockGallery = clone $mockGallery;
     	
     	$mockAlbumRepository = 
$this->getMock('Tx_Yag_Domain_Repository_AlbumRepository', 
array('findAll'), array(), '', FALSE);
 
$mockAlbumRepository->expects($this->once())->method('findAll')->will($this->returnValue(array('album1', 
'album2')));

         $mockView = $this->getMock('Tx_Fluid_Core_View_TemplateView', 
array('assign'), array(), '', FALSE);

         // I'm not really satisfied with this, as it does not matter in 
which order the functions are called,
         // as long as they are called...
         $mockView->expects($this->at(0))
             ->method('assign')
             ->with('availableAlbums', array('album1','album2'));
         $mockView->expects($this->at(1))
             ->method('assign')
             ->with('selectedAlbums', array('album1', 'album2'));
         $mockView->expects($this->at(2))
             ->method('assign')
             ->with('gallery', $clonedMockGallery);

         $mockController = 
$this->getMock($this->buildAccessibleProxy('Tx_Yag_Controller_GalleryController'), 
array('checkForAdminRights'),array(true), '', FALSE);
         $mockController->_set('view', $mockView);
         $mockController->_set('albumRepository', $mockAlbumRepository);

         $mockController->editAction($clonedMockGallery);
     }


And it rocks :-) I will write a longer article on my blog on that 
subject, as soon, as I find some time!

Greetings

Michael




Sebastian Kurfürst schrieb:
> Hi,
> 
>> Do you think, it's a good idea to test controller actions anyway? I
>> thought it would be a nice and easy way, to check, whether the actions
>> are all still running after refactoring for example...
> I'm not 100% sure - but I'd be curious about your experiences once you
> tried it out :-)
> 
> Greets,
> Sebastian


More information about the TYPO3-project-typo3v4mvc mailing list