Skip to content

Commit 71758cf

Browse files
committed
Refer to Capybara.current_session by document
The Capybara DSL provides a `page` helper method to refer to the Capybara.current_session for assertions about the current document. Unfortunately as I have chosen a key application model to be `Page`, I am often shadowing the Capybara.current_session helper in system specs with the local variable `page` to refer to the domain concept. Using them interchangeably doesn’t work. In this changeset, I have added a separate alias to Capybara.current_sesison for system specs called `document`. I’m not enforcing its usage as Capybara appears to use the `page` DSL method internally.
1 parent e10809e commit 71758cf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+209
-213
lines changed

lib/generators/article/templates/article_spec.rb.tt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ RSpec.describe "<%= title %>", type: :system do
44
it "displays the article content" do
55
visit "/articles/<%= article_file_name %>"
66

7-
expect(page).to have_content <%= title %>
7+
expect(document).to have_content <%= title %>
88
end
99
end

spec/requests/feed_spec.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616
get "/feed"
1717

1818
expect(response.status).to eq(200)
19-
page = Capybara.string(response.body)
19+
Capybara.string(response.body)
2020

21-
expect(page).to have_content("Introducing Joy of Rails")
22-
expect(page).to have_content("/introducing-joy-of-rails")
23-
expect(page).to have_content("How it started, How it’s going</h2>")
24-
expect(page).to have_content(%(<div class="code-wrapper highlight language-ruby"><pre><code><span class="k">class</span>))
25-
expect(page).not_to have_content(%(<turbo-frame))
21+
expect(document).to have_content("Introducing Joy of Rails")
22+
expect(document).to have_content("/introducing-joy-of-rails")
23+
expect(document).to have_content("How it started, How it’s going</h2>")
24+
expect(document).to have_content(%(<div class="code-wrapper highlight language-ruby"><pre><code><span class="k">class</span>))
25+
expect(document).not_to have_content(%(<turbo-frame))
2626
end
2727

2828
it "render valid feed" do

spec/requests/search_spec.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,31 @@
77

88
expect(response).to have_http_status(:success)
99

10-
expect(page).not_to have_content("No results")
10+
expect(document).not_to have_content("No results")
1111
end
1212

1313
it "renders empty without search query as POST" do
1414
post search_path
1515

1616
expect(response).to have_http_status(:success)
1717

18-
expect(page).not_to have_content("No results")
18+
expect(document).not_to have_content("No results")
1919
end
2020

2121
it "renders empty without search query as turbo stream" do
2222
get search_path(format: :turbo_stream)
2323

2424
expect(response).to have_http_status(:success)
2525

26-
expect(page).not_to have_content("No results")
26+
expect(document).not_to have_content("No results")
2727
end
2828

2929
it "renders No results feedback when query is long enough" do
3030
get search_path, params: {query: "Pro"}
3131

3232
expect(response).to have_http_status(:success)
3333

34-
expect(page).to have_content("No results")
34+
expect(document).to have_content("No results")
3535
end
3636

3737
it "renders the search results without query" do
@@ -42,7 +42,7 @@
4242

4343
expect(response).to have_http_status(:success)
4444

45-
expect(page).not_to have_content("No results")
45+
expect(document).not_to have_content("No results")
4646
end
4747

4848
it "renders the search results with query as turbo stream" do
@@ -73,15 +73,15 @@
7373

7474
expect(response).to have_http_status(:success)
7575

76-
expect(page).to have_content("Progressive Web Apps on Rails Showcase")
77-
expect(page).not_to have_content("Introducing Joy of Rails")
76+
expect(document).to have_content("Progressive Web Apps on Rails Showcase")
77+
expect(document).not_to have_content("Introducing Joy of Rails")
7878
end
7979

8080
it "doesn’t blow up with invalid query" do
8181
get search_path, params: {query: "(((("}
8282

8383
expect(response).to have_http_status(:success)
84-
expect(page).to have_content("No results")
84+
expect(document).to have_content("No results")
8585
end
8686

8787
it "doesn’t blow up with invalid query as turbo stream" do

spec/requests/share/polls_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111
it "does not render the New Poll button when not allowed" do
1212
get share_polls_url
1313

14-
expect(page).to_not have_content("New Poll")
14+
expect(document).not_to have_content("New Poll")
1515
end
1616

1717
it "renders the New Poll button when allowed" do
1818
Flipper.enable(:polls, login_as_user)
1919
get share_polls_url
2020

21-
expect(page).to have_content("New Poll")
21+
expect(document).to have_content("New Poll")
2222
end
2323
end
2424

spec/requests/share/snippets_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@
2323
it "does not render the New Snippet button when not allowed" do
2424
get share_snippets_url
2525

26-
expect(page).to_not have_content("New Snippet")
26+
expect(document).not_to have_content("New Snippet")
2727
end
2828

2929
it "renders the New Snippet button when allowed" do
3030
Flipper.enable(:snippets, login_as_user)
3131
get share_snippets_url
3232

33-
expect(page).to have_content("New Snippet")
33+
expect(document).to have_content("New Snippet")
3434
end
3535
end
3636

@@ -58,7 +58,7 @@
5858
snippet = FactoryBot.create(:snippet)
5959
get share_snippet_url(snippet)
6060

61-
expect(page).to_not have_content("Edit this snippet")
61+
expect(document).not_to have_content("Edit this snippet")
6262
end
6363

6464
it "renders the Edit Snippet button when allowed" do
@@ -67,7 +67,7 @@
6767
snippet = FactoryBot.create(:snippet, author: user)
6868
get share_snippet_url(snippet)
6969

70-
expect(page).to have_content("Edit this snippet")
70+
expect(document).to have_content("Edit this snippet")
7171
end
7272
end
7373
end

spec/requests/topics_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414

1515
expect(response).to have_http_status(:success)
1616

17-
expect(page).to have_content(topic_1.name)
18-
expect(page).to have_content(topic_2.name)
17+
expect(document).to have_content(topic_1.name)
18+
expect(document).to have_content(topic_2.name)
1919
end
2020
end
2121

spec/support/requests.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module RequestSpecHelpers
2-
def page
3-
@page ||= Capybara.string(response.body)
2+
def document
3+
@document ||= Capybara.string(response.body)
44
end
55
end
66

spec/support/system.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,18 @@
1111
Capybara.default_max_wait_time = 5
1212
Capybara.disable_animation = true
1313

14+
module CapybaraHelper
15+
extend ActiveSupport::Concern
16+
17+
# Use the current session as an alternative to `page`
18+
def document = Capybara.current_session
19+
end
20+
1421
RSpec.configure do |config|
1522
config.include Warden::Test::Helpers, type: :system
1623
config.include Rails.application.routes.url_helpers, type: :system
1724
config.include ActiveJob::TestHelper
25+
config.include CapybaraHelper, type: :system
1826

1927
config.before(:each, type: :system) do
2028
driven_by(:cuprite,

spec/system/admin/newsletter_spec.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@
1818

1919
click_button "Create Newsletter"
2020

21-
expect(page).to have_content("Newsletter was successfully created.")
21+
expect(document).to have_content("Newsletter was successfully created.")
2222

23-
expect(page).to have_content("Welcome to Joy of Rails!")
23+
expect(document).to have_content("Welcome to Joy of Rails!")
2424

2525
click_link "Back to newsletters"
2626

27-
expect(page).to have_content("Welcome to Joy of Rails!")
27+
expect(document).to have_content("Welcome to Joy of Rails!")
2828

2929
within "#newsletters" do
3030
click_link "Edit"
@@ -38,9 +38,9 @@
3838

3939
click_button "Update Newsletter"
4040

41-
expect(page).to have_content("Newsletter was successfully updated.")
41+
expect(document).to have_content("Newsletter was successfully updated.")
4242

43-
expect(page).to have_content("OMG! This is the first newsletter.")
43+
expect(document).to have_content("OMG! This is the first newsletter.")
4444
end
4545

4646
it "send a newsletter test", :vcr do
@@ -58,7 +58,7 @@
5858

5959
click_button "Send Test"
6060

61-
expect(page).to have_content("[TEST] Newsletter was successfully delivered.")
61+
expect(document).to have_content("[TEST] Newsletter was successfully delivered.")
6262

6363
perform_enqueued_jobs_and_subsequently_enqueued_jobs
6464

@@ -88,7 +88,7 @@
8888

8989
click_button "Send Live"
9090

91-
expect(page).to have_content("[LIVE] Newsletter was successfully delivered.")
91+
expect(document).to have_content("[LIVE] Newsletter was successfully delivered.")
9292

9393
perform_enqueued_jobs_and_subsequently_enqueued_jobs
9494

spec/system/admin_users/sessions_spec.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@
55
admin_user = FactoryBot.create(:admin_user)
66

77
visit new_admin_users_session_path
8-
expect(page).to have_text("Sign in to your admin account")
8+
expect(document).to have_text("Sign in to your admin account")
99

1010
fill_in "Email", with: admin_user.email
1111
fill_in "Password", with: "password"
1212

1313
click_button "Sign in"
1414

15-
expect(page).not_to have_text("Sign in to your admin account")
15+
expect(document).not_to have_text("Sign in to your admin account")
1616

17-
expect(page).to have_text("Signed in successfully.")
17+
expect(document).to have_text("Signed in successfully.")
1818

19-
expect(page).to have_text(admin_user.email)
19+
expect(document).to have_text(admin_user.email)
2020
end
2121

2222
it "fails sign in" do
@@ -30,7 +30,7 @@
3030
click_button "Sign in"
3131

3232
expect(current_path).to eq(admin_users_sessions_path)
33-
expect(page).to have_text("Incorrect email or password.")
33+
expect(document).to have_text("Incorrect email or password.")
3434
end
3535

3636
it "signs out admin_user" do
@@ -40,11 +40,11 @@
4040

4141
visit root_path
4242

43-
expect(page).to have_text(admin_user.email)
43+
expect(document).to have_text(admin_user.email)
4444

4545
click_button "Sign out"
4646

47-
expect(page).to have_text("Signed out successfully.")
48-
expect(page).not_to have_text(admin_user.email)
47+
expect(document).to have_text("Signed out successfully.")
48+
expect(document).not_to have_text(admin_user.email)
4949
end
5050
end

0 commit comments

Comments
 (0)