Asantoya17
Asantoya17

Reputation: 4725

no link with title, id or text 'Delete' found

I'm using rspec and I'm trying to test the DELETE method for the user's cars :

 describe "DELETE " do
    it "should delete a car" do
        #@car = Factory.create :car
        expect { click_link "Delete" }.to change{Car.count}.by(-1)
    end
end

when I run it i got this error:

CarsController DELETE  should delete a car
 Failure/Error: expect { click_link "Delete" }.to change{Car.count}.by(-1)
 Capybara::ElementNotFound:
   no link with title, id or text 'Delete' found
 # (eval):2:in `click_link'
 # ./spec/controllers/car_controller_spec.rb:30:in `block (4 levels) in <top (required)>'
 # ./spec/controllers/car_controller_spec.rb:30:in `block (3 levels) in <top (required)>'

and I don't know why becuase I have this link on my index.html.erb:

<td><%= link_to 'Delete', user_car_path(@user, car), :confirm => 'Are you sure?', :method => :delete %></td>

but in my app it works

Upvotes: 1

Views: 291

Answers (1)

Gazler
Gazler

Reputation: 84180

Delete requests in rails get converted to a form which is why the element cannot be found. You will also need to trigger the confirmation.

To click on the Delete:

find('a', :text => 'Delete').click

To accept the confirmation:

page.driver.browser.switch_to.alert.accept

There was a Railscast on implementing the destroy method without JavaScript if you want to be able to use click_link

http://railscasts.com/episodes/77-destroy-without-javascript/

Upvotes: 2

Related Questions