jeudi 18 juin 2020

Laravel Custom FormRequest in PHPUnit env

Okay so, I've tried searching for a while now for solutions on how and why this is happening, here it goes.

On production environment, this chunk of code works just fine.

public function store(Add $request) {
        $data = $request->validated();
        //....  some other stuff
}

Add is a class that extends Illuminate\Foundation\Http\FormRequest, it has some rules only (that I've checked and doesn't influence the error, being the reason for not being posted)

On my test schema, I've got a couple scenarios, mostly Acceptence tests to make sure everything is created (or not) according to different data input. These make calls such as $this->call($uri, $body, $headers) and they work as expected, simulating the production environment.

However, I'm trying to write a test specific to the Controller and it keeps on giving me this error:

$userController = new UserController();
$request = Add::create('/test', 'POST', $body);
$userController->store($request);

This outputs an error:

 Error: Call to a member function validated() on null
 vendor/laravel/framework/src/Illuminate/Foundation/Http/FormRequest.php:188

I've tried to output what dd($request->validated()); returns but this is where it crashes.

  1. What is the solution?
  2. Why it happens?


from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2YP38TF
via IFTTT

Aucun commentaire:

Enregistrer un commentaire