No need to disable middleware, just authenticate before running those tests
$user = new User(array('name' => 'John'));
$this->be($user); //You are now authenticated
...
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 ...
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
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 :)
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community