I'm trying create a test in Laravel 5.1 but I want to use it without the authorization form.
In my RoleController class I put this in order to :
public function __construct()
{
$this->middleware('auth');
}
I've tried using use WithoutMiddleWare;
but did'nt work. This is the phpunit output:
A request to [http://localhost/nuevo/rol] failed. Received status code [500].
Also tried using $this->withoutMiddleware();
for each test method but that din't work either. This is the phpunit output:
InvalidArgumentException: Nothing matched the filter [nombre] CSS query provided for [http://localhost/auth/login].
Instead of visit the "nuevo/rol" route the test make a request to "auth/login" as it works using the auth form.
Is there a method to test without using the authorization form or I need to put in my test code the logic to use it?
To getting started with Test-driven development (TDD) in Laravel, go to the tests directory in the root of your installation (support for PHPUnit testing is provided by default so you need not to worry about setting up as this is already catered for).
To create your test, do run the following Artisan Command:
php artisan make:test RoleTest
The Artisan Command above will create a new RoleTest class in your tests directory with the following test case in it (in your newly created tests/RoleTest.php
file):
class RoleTest extends TestCase
{
/**
* A basic test example.
*
* @return void
*/
public function testExample()
{
$this->assertTrue(true);
}
}
To disable Middleware before running your test, do use use WithoutMiddleware;
as follow so as to apply it to all methods under your RoleTest class:
class RoleTest extends TestCase
{
use WithoutMiddleware;
/**
* A basic test example.
*
* @return void
*/
public function testExample()
{
$this->assertTrue(true);
}
}
In case you want to apply the exemption of the middleware to selected methods only make use of $this->withoutMiddleware();
within such methods as follow:
public function testExampleWithoutMiddleware()
{
$this->withoutMiddleware();
$this->visit('/')
->see('Laravel');
}
Simply run phpunit
in your command line interface to run your tests.
In the last snippet above, the middleware is disabled for the testExampleWithoutMiddleware
method in which we run a test visit to resource available at the root of your site or application (based on your installation directory and or provisions made in your routes.php
file) and check whether it contains the term Laravel
.
TL;DR
Simply use $this->withoutMiddleware();
is your method to run PHPUnit test with your middleware disabled; not $this->middleware('auth');
which rather enforces it.
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