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.
app/Http/Controllerdirectory?