Mattia Lipreri
Mattia Lipreri

Reputation: 953

Test destroy action with capybara and rspec

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!

Upvotes: 3

Views: 4944

Answers (3)

jrmyward
jrmyward

Reputation: 136

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!

Upvotes: 1

AMIC MING
AMIC MING

Reputation: 6354

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

Upvotes: 4

Tom L
Tom L

Reputation: 3409

expect { click_link 'Destroy' }.to change(Category, :count).by(-1)

Upvotes: 15

Related Questions