secret
Last active

RSpec + Capybara as Cucumber replacement.

  • Download Gist
account_management_helpers.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
# -*- encoding : utf-8 -*-
 
# spec/support/features/helpers/account_management_helpers.rb
 
module Features
module Helpers
module AccountManagementHelpers
private
def when_visit_account_page
visit account_path(@account)
end
 
def and_fill_in_account_form_with(data)
within('form#edit_account') do
data.each do |field, value|
fill_in field.to_s.titleize, with: value
end
 
click_button 'Update account'
end
end
 
def user_sees_notice(text)
expect(page).to have_css('div#flash.notice', text: text)
end
 
def it_should_be_forbidden
expect(page.status_code).to be(403)
expect(page).to have_content('Only the account holder can make changes.')
end
end
end
end
login_helpers.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# -*- encoding : utf-8 -*-
 
# spec/support/features/helpers/login_helpers.rb
 
module Features
module Helpers
module LoginHelpers
def login_as(user, password = 'secret')
visit login_path
 
fill_in 'Email Address', with: user.email
fill_in 'Password', with: password
click_button 'Log in'
end
end
end
end
manage_account_spec.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
# -*- encoding : utf-8 -*-
 
# spec/features/manage_account_spec.rb
 
require 'spec_helper'
 
feature 'Account holder manages account' do
include Features::Helpers::AccountManagementHelpers
 
background do
@account = create(:account)
end
scenario 'Account holder changes the account name' do
login_as @account.holder
 
when_visit_account_page
and_fill_in_account_form_with(name: 'Acme Inc.')
 
user_sees_notice(%(Your account has been successfully updated.))
end
scenario 'A common user cannot access to the account management' do
login_as create(:user)
 
when_visit_account_page
it_should_be_forbidden
end
end
spec_helper.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
# -*- encoding : utf-8 -*-
 
# spec/spec_helper.rb
 
ENV["RAILS_ENV"] = 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'capybara/poltergeist'
 
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
 
RSpec.configure do |config|
config.fail_fast = true
config.infer_base_class_for_anonymous_controllers = false
 
with_options(type: :feature) do |rspec|
rspec.config.before(:all) do
Capybara.default_host = 'http://test.host'
Capybara.javascript_driver = :poltergeist
end
 
rspec.config.include FactoryGirl::Syntax::Methods
rspec.config.include Features::Helpers::LoginHelpers
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.