Dirk313's avatar

Laravel update and delete test not passing

i have created a test for my create method to test out my controller but cant seem to see the issue at hand, can any one shed some light please , this is my update controller test

    public function test_if_user_can_update_a_supplier()
{
    //Make fake data, don't persist to database.
    $supplier = Supplier::factory()->make()->setAppends([])->makeHidden(['created_at', 'updated_at']);

    //Create an Admin User and assign the Administrator role to this new user
    $adminUser = factory(AdminUser::class)->create();
    $adminUser->roles()->sync(Role::where('name', 'Administrator')->first());

    //Make the request as the new authenticated Administrator user with the admin guard.
    $this->actingAs($adminUser, config('admin-auth.defaults.guard'))
        ->json(
            'POST',
            route('admin/suppliers/update'),
            $supplier->toArray()
        )
        ->assertStatus(302)
        ->assertRedirectToRoute('admin/suppliers/index');

    $this->assertDatabaseHas(
        'suppliers',
        $supplier->toArray()
    );
}

i get this error when running the test Missing required parameter for [Route: admin/suppliers/update] [URI: admin/suppliers/{supplier}] [Missing parameter: supplier].

But when i add the @supplier->id to

	route('admin/suppliers/update' , $supplier-id ),
            $supplier->toArray()
        )

i still get the same error

This is the delete test that returns the same error

	    public function test_if_a_user_can_delete_a_supplier()
{
    //Make fake data, don't persist to database.
    $supplier = Supplier::factory()->make()->setAppends([])->makeHidden(['created_at', 'updated_at']);

    //Create an Admin User and assign the Administrator role to this new user
    $adminUser = factory(AdminUser::class)->create();
    $adminUser->roles()->sync(Role::where('name', 'Administrator')->first());

    $this->actingAs($adminUser, config('admin-auth.defaults.guard'))
        ->json(
            'DELETE',
            route('admin/suppliers/destroy'),
            $supplier->toArray()
        )
        ->assertStatus(302)
        ->assertRedirectToRoute('admin/suppliers/index');
    $this->assertDatabaseMissing(
        'suppliers',
        $supplier->toArray()

    );
}
0 likes
11 replies
webrobert's avatar

@Dirk313 because your route expects a supplier...

Route::delete('/{supplier}', 'SuppliersController@destroy')->name('destroy');

otherwise you are just hitting a route. How would it know which supplier to delete?

    $this->actingAs($adminUser, config('admin-auth.defaults.guard'))
        ->json(
            'DELETE',
            route('admin/suppliers/destroy'), // no supplier provided
            $supplier->toArray()
        )
        ->assertStatus(302)
        ->assertRedirectToRoute('admin/suppliers/index');
    $this->assertDatabaseMissing(
        'suppliers',
        $supplier->toArray()

    );

so probably

route('admin/suppliers/destroy', $supplier), 
Dirk313's avatar

@webrobert HI haha sorry replied on the other thread i have tried that route but still getting the same error Sir

	'DELETE',
            route('admin/suppliers/destroy', $supplier),
            $supplier->toArray()
Dirk313's avatar

@webrobert this is the same message im getting Missing required parameter for [Route: admin/suppliers/destroy] [URI: admin/suppliers/{supplier}] [Missing parameter: supplier].

webrobert's avatar

@Dirk313 my bad, I misread the code... forget what I wrote before. I haven't tested with ->json before but that is clearly where the issue is.

Dirk313's avatar

@webrobert no problem, you are right that is be the issue but to get that darn thing sorted is n pain in the behind now hehe becuase the "Missing required parameter for [Route: admin/suppliers/destroy] [URI: admin/suppliers/{supplier}] [Missing parameter: supplier]", means that the supplier parameter is still not being passed correctly to the route.

webrobert's avatar

shouldn't it be...

  ->post(
   route('admin/suppliers/destroy'),
   $supplier->toArray()
)
Dirk313's avatar

@webrobert I also just tried this now but still getting the same error

 $this->actingAs($adminUser, config('admin-auth.defaults.guard'));
        $this->post(route('admin/suppliers/destroy', $supplier),
            $supplier->toArray()
        )

and also tried this

 $this->actingAs($adminUser, config('admin-auth.defaults.guard'));																								 
       $this->post(route('admin/suppliers/destroy'),
            $supplier->toArray()
        )
Dirk313's avatar

@webrobert i managed to resolve the issue i changed ->make() to ->create(), But here is the question when testing your update and delete method in the controllers, is it correct to use the create method to persist data to the db ? or should you stick with the make method when creating ?

I have created the create method test where i used the make method so im just wondering when should you use make and create

	    public function test_if_user_can_update_a_supplier()
{
    //Make fake data, persist to database.
    $supplier = Supplier::factory()->create()->setAppends([])->makeHidden(['created_at', 'updated_at']);

    //Create an Admin User and assign the Administrator role to this new user
    $adminUser = factory(AdminUser::class)->create();
    $adminUser->roles()->sync(Role::where('name', 'Administrator')->first());

    //Make the request as the new authenticated Administrator user with the admin guard.
    $this->actingAs($adminUser, config('admin-auth.defaults.guard'))
        ->json(
            'POST',
            route('admin/suppliers/update' , $supplier),
            $supplier->toArray()
        )
        ->assertStatus(302)
        ->assertRedirect('admin/suppliers');

    $this->assertDatabaseHas(
        'suppliers',
        $supplier->toArray()
    );

}
webrobert's avatar

@Dirk313 ah well there ya go. yes make doesn't add it to the database. So in this case you CANT delete something that doesn't exist.

1 like
Dirk313's avatar

@webrobert sorry this is a noob question so when using make na data would be able to b tested but therefore using create data could be created and therefore tested if that still exist in the db

webrobert's avatar
Level 51

@Dirk313 so make just makes a new model. So if your dd it it’s a model but it’s not saved. So if you are testing if something is in the database it will fail cause it’s not. And it won’t have an id yet.

dd and have a look

Basically new model()

Please or to participate in this conversation.