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!

5
  • I think this might help: stackoverflow.com/a/50709981/9920875, because I also think you could be passing in the wrong request namespace instead. Commented Nov 4, 2020 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 . Commented Nov 4, 2020 at 18:45
  • @Johhn if that were the case, PHP would complain about the parameter not matching the function signature Commented Nov 4, 2020 at 18:45
  • What is $this->user and where is it defined? Please add this code to your question. Commented Nov 4, 2020 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 . Commented Nov 4, 2020 at 19:02

1 Answer 1

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');
} 
Sign up to request clarification or add additional context in comments.

3 Comments

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');
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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.