简体   繁体   中英

I get No route matches [DELETE] "/" when testing click on delete link with capybara, works outside of system test

This is my delete link:

<%= link_to "delete", client, data: {
          "turbo-method": :delete,
          "turbo-confirm": "Are you sure?" } %>

This is the system test using Minitest / Capybara / Selenium / Rails 7:

test "delete client" do
    accept_confirm do
      first("ul.clients li#client-#{@client.id} span.client-controls a").click
    end
    assert_selector "div.alert-success", text: "Client is deleted!"
  end

When I run the test I get ActionController::RoutingError: No route matches [DELETE] "/"

When I tried:

puts first("ul.clients li#client-#{@client.id} span.client-controls a")["href"]

I got the correct url (/clients/123). I read about this issue that Capybara didn't accept turbo methods, but it should have been solved with this: https://github.com/teamcapybara/capybara/commit/cc451e77b2fb6588e1d00814d0361e63bb66b86c

I'm using Rails 7, capybara (3.37.1), selenium-webdriver (4.1.0), minitest (5.15.0)

EDIT for more info: just checked test.log:

Started DELETE "/clients/298486374" for 127.0.0.1 at 2022-09-10 13:54:24 +0200
Processing by ClientsController#destroy as TURBO_STREAM
  Parameters: {"id"=>"298486374"}

... delete query does it's work just fine...

Redirected to http://127.0.0.1:44597/
Completed 302 Found in 10ms (ActiveRecord: 1.2ms | Allocations: 1962)
Started DELETE "/" for 127.0.0.1 at 2022-09-10 13:54:25 +0200

Why is DELETE "/" requested after redirect????

My destroy action in ClientsController:

def destroy
    get_client.destroy
    flash[:success] = "Client is deleted!"
    redirect_to root_url
  end

  private

    def get_client
      Client.find(params[:id])
    end

So... yea... I guess I was too quick to post a question here... But, for anyone else having the same problem here's the solution:

I just added status: :see_other after redirect_to root_url in my destroy action and now it works fine.

I got the idea from this old post: https://makandracards.com/makandra/38347-redirecting-responses-for-patch-or-delete-will-not-redirect-with-get

Still not sure exactly why it works the way it does so if anyone wants to contribute - please do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM