Commit 2bfecd8

mo khan <mo@mokhan.ca>
2014-11-16 02:10:25
extract dashboard page model class and some helpers.
1 parent 003d45b
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
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
spec/support/pages/dashboard_page.rb
@@ -0,0 +1,5 @@
+class DashboardPage < PageModel
+  def initialize
+    super(root_path)
+  end
+end
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
-