7

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?

3
  • Where are you creating your test? In your app/Http/Controller directory? Commented Sep 21, 2016 at 19:36
  • No, in the controller the only thing I do is load in the constructor the auth middleware. My test is on the tests directory and there is where I tried to disable the middleware. Commented Sep 21, 2016 at 20:30
  • Ok. Just posted a solution below. Commented Sep 21, 2016 at 20:46

1 Answer 1

10

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Maybe I did not explain well. I need the $this->middleware('auth'); in the controller because I want that to access is needed to be logged, and in the test I want to bypass the authorization but I've already tried the both methods that you explain but none of it worked.
Your question says without passing through auth! What do you want to achieve? Test access to the page with middleware enabled and login as a user for that purpose or test access to the page with middleware enabled so as not to login in?
This throws de exception PHP Error: Call to a member function instance() on null in /var/www/html/app/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.