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.
If you've writing custom filters, routes, etc, you should unit test them, but not as part of your tests on a particular controller action. They should be tested in isolation.
Feature and Unit tests are methods of software testing that isolate a part of a system and test it. A unit test could look at something small like a single method e.g. adding a row to a database, whereas a feature test will have a broader view such as registering a user and validating their details.
Use Laravel Unit Testing to Avoid Project-Wrecking Mistakes. Pardeep Kumar. 7 Min Read. PHPUnit is one of the most well known and highly optimized unit testing packages of PHP. It is a top choice of many developers for rectifying different developmental loopholes of the application.
You can call your actions directly:
$response = $this->action('GET', 'OrdersController@show', ['id' => 1]);
The action
method has been removed from Laravel 5.4 testing api
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With