1

I am very new to testing in Laravel, I'm working in laravel 8, the function that I want to test is :

When I run my test I get this error:

ErrorException: Trying to get property 'id' of non-object

Thank you for any help!

SouhiStack
  • 17
  • 7
  • I think this might help: https://stackoverflow.com/a/50709981/9920875, because I also think you could be passing in the wrong request namespace instead. – Johhn Nov 04 '20 at 18:31
  • Thank you but I defined the "use Illuminate\Http\Request;" in my test function and I don't think that should be the problem . – SouhiStack Nov 04 '20 at 18:45
  • @Johhn if that were the case, PHP would complain about the parameter not matching the function signature – miken32 Nov 04 '20 at 18:45
  • What is `$this->user` and where is it defined? Please add this code to your question. – miken32 Nov 04 '20 at 18:46
  • each order has a user_id and in order to create the order we need to make etablish the connection with a user , I order to generate the user I used a "userFactory" wich generate for me the users while testing . – SouhiStack Nov 04 '20 at 19:02

1 Answers1

1

Usually you don't even need to use the Request class in your tests.

public function testStore()
{
    Sanctum::actingAs($this->user, ['*']);

    $response = $this->postJson('/order/store', [
        'doAssigned' => false,
        'doValidate' => false,
    ]);

    $response->assertJsonPath('status', 'draft');
} 
IGP
  • 14,160
  • 4
  • 26
  • 43
  • Thank you so much for your help , but I didn't understand what do you mean by "the endpoint" , in my 'api.php' I have : `Route::apiResource('order', 'OrderController');` – SouhiStack Nov 05 '20 at 09:53
  • In my `api.php` I added the line `Route::get('order/store', 'OrderController@store');` but I got the error `InvalidArgumentException: Action App\Http\Controllers\App\Http\Controllers\OrderController@store not defined.` – SouhiStack Nov 05 '20 at 10:08
  • The endpoint is `order/store`. The error you got has to do with namespaces. Instead of `action('App\Http\Controllers\OrderController@store')` you could try just `action('OrderController@store')`, but you could just put the endpoint directly in the test. I just edited it. – IGP Nov 05 '20 at 15:55