Support the ongoing development of Laravel.io →
Authentication Requests Testing
Last updated 2 years ago.
0

Same problem, don't know how to disable middleware

0

No need to disable middleware, just authenticate before running those tests

$user = new User(array('name' => 'John'));
$this->be($user); //You are now authenticated
...

http://laravel.com/docs/5.0/testing#helper-methods

Last updated 9 years ago.
0

It works like a charm. Thanks a lot!

0

I initially tried to perform the test without middleware, however I kept running into an error where I could not call anything from Auth::user() - clearly because the user was not logged in. I could have added a small check around it, but this did not validate my test. After being able to authenticate as a user in the unit test, everything was solved and the test worked perfectly.

Thank you for your suggestion!

mackiecarr said:

No need to disable middleware, just authenticate before running those tests

$user = new User(array('name' => 'John'));
$this->be($user); //You are now authenticated
...

http://laravel.com/docs/5.0/testing#helper-methods

0

mackiecarr said:

No need to disable middleware, just authenticate before running those tests

$user = new User(array('name' => 'John'));
$this->be($user); //You are now authenticated
...

http://laravel.com/docs/5.0/testing#helper-methods My user.php extends Eloquent\Model so the error message "must be an instance of Illuminate\Contracts\Auth\Authenticatable, instance of App\User given" given. Could you help me about it? or how to use another methods to avoid auth middleware

0

Hi, Anca. Maybe it help you. You can except some middlewares for some methods for controllers on serialization __construct() method. Example:

<?php namespace App\Http\Controllers;

class MyController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth', ['except' => ['myMethod1', 'myMethod2']]); // now middleware auth will except this methods
    }

    public function myMethod1()
    {
        // myMethod1 body
    }

    public function myMethod2()
    {
        // myMethod2 body
    }
}

However it works globaly, so you need to add some condition for $this->middleware('auth', ['except' => ['myMethod1', 'myMethod2']]); I mean enable this declaration only for unit test.

Hope it help :)

Last updated 8 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

anca anca Joined 26 Jan 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.