diff options
| author | mo khan <mo@mokhan.ca> | 2014-11-15 19:10:25 -0700 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2014-11-15 19:10:25 -0700 |
| commit | 2bfecd8585b6171e87c61865100c2bc442ea3b0e (patch) | |
| tree | fe437f8c1080d701a5bcc390ba2cbc95dea8133c | |
| parent | 003d45b91e6f60a27ce7418c89438eb61cab250c (diff) | |
extract dashboard page model class and some helpers.
| -rw-r--r-- | spec/features/login_spec.rb | 18 | ||||
| -rw-r--r-- | spec/support/pages/_page_model.rb | 26 | ||||
| -rw-r--r-- | spec/support/pages/dashboard_page.rb | 5 | ||||
| -rw-r--r-- | spec/support/pages/login_page.rb | 9 |
4 files changed, 44 insertions, 14 deletions
diff --git a/spec/features/login_spec.rb b/spec/features/login_spec.rb index 3eb9ad4..42f65a8 100644 --- a/spec/features/login_spec.rb +++ b/spec/features/login_spec.rb @@ -3,28 +3,28 @@ require 'rails_helper' describe "the signin process", type: :feature do let!(:user) { create(:user, password: 'password') } let(:login_page) { LoginPage.new } + let(:dashboard_page) { DashboardPage.new } context "when the credentials are correct" do it 'signs the user in' do - login_page.login_with(email: user.email, password: 'password') - expect(page).to have_content("Dashboard") - expect(current_path).to eql(root_path) + login_page.visit_page.login_with(email: user.email, password: 'password') + expect(dashboard_page).to be_on_page end end context "when the password is incorrect" do it 'displays an error' do - login_page.login_with(email: user.email, password: 'wrong') - expect(page).to have_content(I18n.translate(:invalid_credentials)) - expect(current_path).to eql(new_session_path) + login_page.visit_page.login_with(email: user.email, password: 'wrong') + expect(login_page).to be_on_page + expect(login_page).to have_error(:invalid_credentials) end end context "when the email is unknown" do it 'displays an error' do - login_page.login_with(email: 'test@example.com', password: 'password') - expect(page).to have_content(I18n.translate(:invalid_credentials)) - expect(current_path).to eql(new_session_path) + login_page.visit_page.login_with(email: 'test@example.com', password: 'password') + expect(login_page).to be_on_page + expect(login_page).to have_error(:invalid_credentials) end end end diff --git a/spec/support/pages/_page_model.rb b/spec/support/pages/_page_model.rb new file mode 100644 index 0000000..3fbace2 --- /dev/null +++ b/spec/support/pages/_page_model.rb @@ -0,0 +1,26 @@ +class PageModel + include Capybara::DSL + include Rails.application.routes.url_helpers + attr_reader :page_path + + def initialize(page_path) + @page_path = page_path + end + + def visit_page + visit page_path + self + end + + def on_page? + current_path == page_path + end + + def has_error?(translation_key) + page.has_content?(translate(translation_key)) + end + + def translate(key) + I18n.translate(key) + end +end diff --git a/spec/support/pages/dashboard_page.rb b/spec/support/pages/dashboard_page.rb new file mode 100644 index 0000000..53ba885 --- /dev/null +++ b/spec/support/pages/dashboard_page.rb @@ -0,0 +1,5 @@ +class DashboardPage < PageModel + def initialize + super(root_path) + end +end diff --git a/spec/support/pages/login_page.rb b/spec/support/pages/login_page.rb index 616603a..db63d51 100644 --- a/spec/support/pages/login_page.rb +++ b/spec/support/pages/login_page.rb @@ -1,9 +1,9 @@ -class LoginPage - include Capybara::DSL - include Rails.application.routes.url_helpers +class LoginPage < PageModel + def initialize + super(new_session_path) + end def login_with(email:, password:) - visit root_path within ".form-signin" do fill_in 'email', with: email fill_in 'password', with: password @@ -11,4 +11,3 @@ class LoginPage click_button "Sign in" end end - |
