Ticket Details

A way to load the routes while testing a controller

ENHANCEMENT Ticket (pending)

###What's going on
- you write a test for your controller
- and realize that your routes aren't loaded.

####Example
{{{
<?php
namespace app\tests\cases\controllers;

use lithium\action\Request;
use app\controllers\BlogController;

class BlogControllerTest extends \lithium\test\Unit
{
    public function test_index()
    {
        $request = new Request();
        $controller = new BlogController(compact('request'));

        $response = $controller($request, $request->params);
        // …
    }
}
}}}

This breaks because, I'm generating some URL into my view and they aren't found because my `app/config/routes.php` wasn't loaded.

####Dirty fix

{{{
require_once LITHIUM_APP_PATH . '/config/routes.php';
}}}


###What was expected
- to either have a way to load and *unload* the routes
- or that the app (and libraries) routes were already loaded.

###Maybe
- maybe these aren't Unit tests and require something better than `\lithium\test\Unit`
- maybe a lot more is required to be able to do proper Controller testing like fixtures, …

TA
on 04.13.11 reported by: greut

Updates

on 04.13.11 by daschl
Hi greut,

i think you are testing the controllers the wrong way. You may want to check how the controllers are tested in the core, just to get a feeling how action invocation works.

http://rad-dev.org/lithium/source/libraries/lithium/tests/cases/action/ControllerTest.php#numbers

Next, if you want to "mock" a request you'll need to pass it a few more params to fill it with all information it needs.

http://rad-dev.org/lithium/source/libraries/lithium/tests/cases/action/RequestTest.php#numbers

Normally, your routes should be loaded already (otherwise the router wouldn't know how to route your /test route!). If you want to inspeect the router and its routes, use the Router class directly and work with it:

http://rad-dev.org/lithium/source/libraries/lithium/tests/cases/net/http/RouterTest.php#numbers

Regards, Mike
on 04.13.11 by greut
Okay, I got your point…

Why not creating a sample tests for the [controller Pages](http://rad-dev.org/lithium/source/app/controllers/PagesController.php) and the [controller HelloWorld](http://rad-dev.org/lithium/source/app/controllers/HelloWorldController.php) there: [app\tests\cases\controllers](http://rad-dev.org/lithium/source/app/tests/cases/controllers)? It'd be very helpful.