Commit 6489265

mo khan <mo@mokhan.ca>
2015-01-20 05:03:45
extract page object to remove some duplication.
1 parent 81976dd
spec/features/registrations_spec.rb
@@ -1,17 +1,15 @@
 require "rails_helper"
 
 feature "Registrations", type: :feature do
+  subject { NewRegistrationPage.new }
+
+  before :each do
+    subject.visit_page
+  end
+
   describe "creating a new account" do
     it 'registers a new account' do
-      visit root_path
-      click_link "Register"
-      within "#new_user" do
-        fill_in "Username", with: 'mo'
-        fill_in "Email", with: 'mo@example.com'
-        fill_in "Password", with: 'password'
-        check "I Agree"
-        click_button "Register"
-      end
+      subject.register_with(username: 'mo', email: 'mo@example.com', password: 'password')
 
       expect(current_path).to eql(dashboard_path)
     end
@@ -20,17 +18,18 @@ feature "Registrations", type: :feature do
       let!(:user) { create(:user) }
 
       it 'displays an error' do
-        visit root_path
-        click_link "Register"
-        within "#new_user" do
-          fill_in "Username", with: user.username
-          fill_in "Email", with: user.email
-          fill_in "Password", with: 'password'
-          check "I Agree"
-          click_button "Register"
-        end
+        subject.register_with(username: user.username)
 
         expect(page).to have_content("Username has already been taken")
+      end
+    end
+
+    context "when the email is taken" do
+      let!(:user) { create(:user) }
+
+      it 'displays an error' do
+        subject.register_with(email: user.email)
+
         expect(page).to have_content("Email has already been taken")
       end
     end
spec/support/pages/new_registration_page.rb
@@ -0,0 +1,20 @@
+require_relative '../page_model.rb'
+
+class NewRegistrationPage < PageModel
+  def initialize
+    super(new_registration_path)
+  end
+
+  def register_with(username: Faker::Internet.user_name,
+                    email: Faker::Internet.email,
+                    password: 'password',
+                    terms: true)
+    within "#new_user" do
+      fill_in "Username", with: username
+      fill_in "Email", with: email
+      fill_in "Password", with: password
+      check "I Agree"
+      click_button "Register"
+    end
+  end
+end
spec/support/page_model.rb
@@ -0,0 +1,18 @@
+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
+end
spec/rails_helper.rb
@@ -19,7 +19,7 @@ require 'capybara/poltergeist'
 # directory. Alternatively, in the individual `*_spec.rb` files, manually
 # require only the support files necessary.
 #
-# Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
+Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
 
 # Checks for pending migrations before tests are run.
 # If you are not using ActiveRecord, you can remove this line.