3
votes

I'm under capybara and rspec, which is the best way to test the destroy link for an object? I have to click this link

<a href="/categories/1" data-confirm="Are you sure?" data-method="delete" rel="nofollow">Destroy</a>

but I'm not able to select it with:

it "should destroy a category" do
  visit categories_path
  find(:xpath, '//link[@href="/categories/1"]').click
  # how to handle javascript pop up for confirmation?
end

any hint? Thank you!

3

3 Answers

15
votes
expect { click_link 'Destroy' }.to change(Category, :count).by(-1)
4
votes
it "should destroy a category" do
 expect { click_link('Destroy') }.to change(Category, :count).by(-1)
end
1
votes

Since it looks as if this is a request spec, you can more closely simulate the user with:

it "should destroy a category" do
  visit categories_path
  find(:xpath, '//link[@href="/categories/1"]').click
  sleep 1.seconds
  alert = page.driver.browser.switch_to.alert
  expect { alert.accept }.to change(Category, :count).by(-1)
end

Cheers!