2016-09-19 183 views
0

我正在爲插件中的控制器編寫測試(Assets插件)。CakePHP 3:測試控制器

這是控制器:

namespace Assets\Controller; 

use Cake\Controller\Controller; 

class AssetsController extends Controller 
{ 
    public function asset($filename, $type) 
    { 
     $this->response->type($type); 
     $this->response->file(ASSETS . DS . $filename); 

     return $this->response; 
    } 
} 

正如你所看到的,它只能發送一個資源文件。

這是路線:

Router::plugin('Assets', ['path' => '/assets'], function ($routes) { 
    $routes->connect(
     '/:type/:filename', 
     ['controller' => 'Assets', 'action' => 'asset'], 
     [ 
      'type' => '(css|js)', 
      'filename' => '[a-z0-9]+\.(css|js)', 
      'pass' => ['filename', 'type'], 
     ] 
    ); 
}); 

這是測試類:

namespace Assets\Test\TestCase\Controller; 

use Assets\Utility\AssetsCreator; 
use Cake\TestSuite\IntegrationTestCase; 

class AssetsControllerTest extends IntegrationTestCase 
{  
    public function testAsset() 
    { 
     //This is the filename 
     $filename = sprintf('%s.%s', AssetsCreator::css('test'), 'css'); 

     $this->get(sprintf('/assets/css/%s', $filename)); 

     $this->assertResponseOk(); 
    } 
} 

運行測試,但是,產生這種異常(全測試here):

1) Assets\Test\TestCase\Controller\AssetsControllerTest::testAsset 
Cake\Routing\Exception\MissingControllerException: Controller class could not be found. in /home/mirko/Libs/Plugins/Assets/vendor/cakephp/cakephp/src/Http/ControllerFactory.php:91 

我不認爲是一個破碎的問題,因爲這樣做會產生同樣的異常:

$url = \Cake\Routing\Router::url([ 
    'controller' => 'Assets', 
    'action' => 'asset', 
    'plugin' => 'Assets', 
    'type' => 'css', 
    'filename' => $filename, 
]); 

$this->get($url); 

我在哪裏做錯了?謝謝。

+0

這是什麼版本的蛋糕3? – systematical

+0

@systematical,版本3.3.3 –

+0

它似乎找不到'Cake \ Controller \ Controller' ...爲什麼? –

回答

1

解決!在我的測試引導,我錯過了:

DispatcherFactory::add('Routing'); 
DispatcherFactory::add('ControllerFactory'); 

現在它的工作。