#Expected response status code [201, 301, 302, 303, 307, 308] but received 404.

52 messages · Page 1 of 1 (latest)

vapid quartz
#

Not sure why I am receiving this error, spent too much time trying to figure it out. My test case:

 public function test_cart_total_price_updates_upon_removing_a_product()
    {
        $user = User::factory()->create();
        $carts = Cart::factory()->count(4)->create(['user_id' => $user->id]);

        $this->actingAs($user);

        $response = $this->get('/cart');

        $response->assertStatus(200);
        
        // Get the total cart price
        $cartTotal = CartController::getTotalCartPrice(); 

        // Make sure the total price is shown in the cart page
        $response->assertSee($cartTotal);

        // Get 1 cart that matches with user 
        $cart = Cart::where('user_id', $user->id)->first();

        // Delete a cart
        $response = $this->post('cart.destroy', [$cart]);

        $response->assertRedirect('/cart');

    }

Route

   Route::resource('/cart', CartController::class); 

Controller

public function destroy(Cart $cart)
    {
        $cart->delete();

        return redirect()->route('cart.index')->with('alert', 'Deleted product from cart');
    }
#

Expected response status code [201, 301, 302, 303, 307, 308] but received 404.

#

The test crashes at the last part where it should redirect back to /cart I've also tried changing to /cart.index

clever knoll
#

You've defined your resource route incorrectly

tame garden
#

You’re using route names in your test, i.e. $this->post('cart.destroy'). Those methods take URIs; not route names.

#

You also shouldn’t be doing multiple requests in a single test case.

clever knoll
rotund wasp
#

Shouldn't you be using $this->delete(...) for a delete request?

#

Also I don't think its good practice to test different routes within the same feature test IMO

vapid quartz
#

So I tried it this way:

$response = $this->post(url('cart.destroy/{$cart}'), [$cart]);

AND 

$response = $this->post(url('cart.destroy, {$cart}'), [$cart]);

But same error.

nimble yarrow
#

Try route helper function not url

vapid quartz
#

Sorry, i got mixed up

#

I've tried route as well

#

replacing url with route

rotund wasp
#

You ignored me 😢
$this->delete(route('cart.destroy', $cart))

nimble yarrow
#

If you are confused, you can do php artisan route:list to see all your routes. Then you'll know the correct HTTP method and url

vapid quartz
hoary arch
#

im no expert but when i get 404 its because i havent run php artisan route:cache after changin the route

rotund wasp
vapid quartz
rotund wasp
rotund wasp
hoary arch
rotund wasp
vapid quartz
#
 GET|HEAD        cart/create ............ cart.create › CartController@create
  GET|HEAD        cart/{cart} ................ cart.show › CartController@show
  PUT|PATCH       cart/{cart} ............ cart.update › CartController@update
  DELETE          cart/{cart} .......... cart.destroy › CartController@destroy
  GET|HEAD        cart/{cart}/edit ........... cart.edit › CartController@edit
rotund wasp
#

Can you run php artisan route:clear

#

As far as I can tell this should be hitting the destroy function in your controller.
$this->delete(route('cart.destroy', $cart))

hoary arch
rotund wasp
#

Clear deletes the cache

vapid quartz
#

I tried to clear and try again but still same error

rotund wasp
vapid quartz
#

The only middleware I have is auth but I am doing actingAs($user) in the test

rotund wasp
#

See if it prints when you run the test

vapid quartz
#

yeah it does

rotund wasp
# vapid quartz yeah it does

Ok can you stick a dump on the end see what you get:
$this->delete(route('cart.destroy', $cart))->dump()

It should be a short bit of html with the redirect info. If its a full html page just tell us.

rotund wasp
vapid quartz
#

nope

rotund wasp
#

lol something weird going on here

#

Can you make it your only request in the test.

vapid quartz
#

kk

#

same thing

#

ah this time it outputted something

tame garden
#

Like I say, you shouldn’t be doing multiple requests in a single test case. Laravel doesn’t make real requests; they’re put directly through an application instance and it’s not cleared between “requests”.

#

One test case. One request.

rotund wasp
#

Yep figured that it must use a static property for some of this

#

Or an instance

tame garden
#

So, if your test case is test_cart_total_price_updates_upon_removing_a_product, you should have some sort of cart object. Create the database records, remove the object, then assert the new total.

public function test_cart_total_price_updates_upon_removing_a_product(): void
{
    // Create cart with items here
    // Assert total

    // Remove item from cart

    // Assert new total
}

If you have a proper cart object, then you should be able to interact with that instead of doing it through requests.

public function test_cart_total_price_updates_upon_removing_a_product(): void
{
    // Pseudo-code
    $cart = new Cart();
    $item = $cart->addItem($someItemThatCosts10Dollars);

    $this->assertEquals(1000, $cart->getTotal());

    $cart->removeItem($item);

    $cart->assertEquals(0, $cart->getTotal();
}