Skip to content

Commit 4b1f3cf

Browse files
authored
Reliable way to check a path with Capybara (#2696)
I just saw a random failure at `./spec/features/products_index_spec.rb:22`. It's a JS-enabled example with an expectation on `current_path`. This is one of those cases where the previous action (in this case clicking on a link) does not necessarily happen immediately, requiring a slight wait, and therefore failing randomly. I have changed all instances of this type of expectation to use `have_current_path`, which is provided by Capybara and applies its [waiting behaviour](https://github.com/teamcapybara/capybara?tab=readme-ov-file#asynchronous-javascript-ajax-and-friends) where appropriate. Note that not all cases actually need it, as this is only a problem in JS-enabled examples, but it's good to be consistent so I changed all.
1 parent 4efb849 commit 4b1f3cf

File tree

4 files changed

+9
-9
lines changed

4 files changed

+9
-9
lines changed

spec/features/index_page_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@
4343
visit admin_customers_path
4444
click_on "Edit"
4545

46-
expect(current_path).to eq(edit_admin_customer_path(customer))
46+
expect(page).to have_current_path(edit_admin_customer_path(customer))
4747
end
4848

4949
it "links to the new page" do
5050
visit admin_customers_path
5151
click_on("New customer")
5252

53-
expect(current_path).to eq(new_admin_customer_path)
53+
expect(page).to have_current_path(new_admin_customer_path)
5454
end
5555

5656
it "displays translated labels" do

spec/features/log_entries_index_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@
3838
visit admin_log_entries_path
3939
click_on t("administrate.actions.edit")
4040

41-
expect(current_path).to eq(edit_admin_log_entry_path(log_entry))
41+
expect(page).to have_current_path(edit_admin_log_entry_path(log_entry))
4242
end
4343

4444
scenario "user clicks through to the new page" do
4545
visit admin_log_entries_path
4646
click_on("New log entry")
4747

48-
expect(current_path).to eq(new_admin_log_entry_path)
48+
expect(page).to have_current_path(new_admin_log_entry_path)
4949
end
5050

5151
scenario "user deletes record", js: true do

spec/features/orders_index_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@
3535
visit admin_orders_path
3636
click_on t("administrate.actions.edit")
3737

38-
expect(current_path).to eq(edit_admin_order_path(order))
38+
expect(page).to have_current_path(edit_admin_order_path(order))
3939
end
4040

4141
scenario "user clicks through to the new page" do
4242
visit admin_orders_path
4343
click_on("New order")
4444

45-
expect(current_path).to eq(new_admin_order_path)
45+
expect(page).to have_current_path(new_admin_order_path)
4646
end
4747

4848
scenario "user deletes record", js: true do

spec/features/products_index_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
expect(page).to have_content(product.name)
2121
expect(page).to have_content(product.description)
22-
expect(current_path).to eq(admin_product_path(product))
22+
expect(page).to have_current_path(admin_product_path(product))
2323
end
2424

2525
it "links to the edit page" do
@@ -28,14 +28,14 @@
2828
visit admin_products_path
2929
click_on "Edit"
3030

31-
expect(current_path).to eq(edit_admin_product_path(product))
31+
expect(page).to have_current_path(edit_admin_product_path(product))
3232
end
3333

3434
it "links to the new page" do
3535
visit admin_products_path
3636
click_on("New product")
3737

38-
expect(current_path).to eq(new_admin_product_path)
38+
expect(page).to have_current_path(new_admin_product_path)
3939
end
4040

4141
scenario "product sorted by has_one association" do

0 commit comments

Comments
 (0)