Summer Sale! All accounts are 50% off this week.

PetroGromovo's avatar

Why making tests passed parameter into method is empty ?

Making http test in laravel 8 in control I create $itemObject object with factory and pass it for edit method

use Tests\TestCase;

class ControllersItemObjectController extends TestCase
{
    use DatabaseMigrations;

    /** @test */
    public function edit_item_object()
    {
        $this->withoutMiddleware();
        $itemObject = ItemObject::factory()->create();

        \Log::info(json_encode($itemObject);  // I see valid object logged
        \Log::info(gettype($itemObject)); // it shows "object"

        $this->asAdmin()->get(route('item-objects.edit', ['item_object' => $itemObject]));

and I got error :
 Missing required parameter for [Route: item-objects.update] [URI: item-objects/{item_object}] [Missing parameter: item_object]. (View:
 /resources/views/item-objects/edit.blade.php)

in routes I have :

|        | GET|HEAD  | item-objects/{item_object}/edit             | item-objects.edit               | App\Http\Controllers\ItemObjectController@edit                        | web                                          |

But in ItemObjectController controller I have :

public function edit(ItemObject $itemObject)
 {
     \Log::info(  json_encode($itemObject) );   // but it returns empty array
     return view('item-objects.edit', [
         ...
     ]);
 }

As parameter $itemObject above is empty it raise error in /resources/views/item-objects/edit.blade.php template above. But I do not see why this parameter is empty ? Where I lose this value passing valid object into ItemObjectController.edit method?

If there is a way to debug it ? I can not use TELESCOPE for tests...

In route parameter is "item_object", but var name is "$itemObject" I do not see wht $itemObject is empty ...

Thanks in advance!

0 likes
3 replies
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

Try

$this->asAdmin()->get(route('item-objects.edit', ['item_object' => $itemObject->id]));

And

public function edit(ItemObject $item_object) //need to match route 
 {
     \Log::info(  json_encode($item_object) );   // but it returns empty array
     return view('item-objects.edit', [
         ...
     ]);
 }
1 like
PetroGromovo's avatar

I tried and it did not help. I can not understand why ... Any debugging tools ?

Please or to participate in this conversation.