Search code examples
unit-testinglaravelphpunitlaravel-4

What's the best way to unit test a controller in Laravel without testing the route too


I've read lots of documentation about testing controllers using $this->call($destination, $parameters, 'GET'); but this seems to rely on the route being set up too, and knowing the right $destination to use.

Generally this is OK, but accessing a controller from a route doesn't seem right for unit testing. I want to unit test the controller, not the route. Is there a standard way to unit test controllers, without dealing with routes?

Is simply manually instantiating the controller and calling the method enough? E.g.

$controller = new MyController;
$response = $controller->someMethod($param);
$this->assertSomething($response);

Perhaps controllers shouldn't be unit tested (and only have acceptance tests) and my request is a sign that my controllers are too heavy.


Solution

  • You can call your actions directly:

    $response = $this->action('GET', 'OrdersController@show', ['id' => 1]);