Morning / Afternoon / Evening,
I'm wondering if someone else has encountered a way to ensure that Middleware is running on your tests.
We're currently doing a lot of Auth::check() & Auth::user()-> checks in our methods, but we're moving to using Middleware on our constructors like so;
public function __construct() {
parent::__construct();
$this->middleware(['auth']);
}
This is working just fine in real use cases, but inside the tests it's not working as intended.
I have the following method;
public function method() {
if(Auth::user()->can('doSomething')) {
}
}
When inside my test I do the following;
$request = Request::create('/route', 'GET', []);
$controller = new MyController();
$response = $controller->method($request);
But the test is complaining that the Auth::user() is null. Now, this is actually my intention. I'm wanting to write a test to ensure that my method is not accessible by those not authenticated in my application.
I was under the impression that even with calling $controller->method(); the __construct() should still be called, and so the Middleware should then handle the situation, and return the correct response.
Am I missing something obvious? I feel like I'm going around in circles going crazy.
Any help would be appreciated. I have had a search online, and even here on Laracasts, but I am unable to find something related to wanting to explicitly make sure that middleware is running on my tests.
Update: My issue has been solved, thank you.
https://discord.software/ https://omegle.onl/