2016-09-19 141 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'); 

现在它的工作。