Skip to content

Instantly share code, notes, and snippets.

@jodosha
Last active December 11, 2015 19:19
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jodosha/eb8379a8969ec3b5c1d9 to your computer and use it in GitHub Desktop.
Save jodosha/eb8379a8969ec3b5c1d9 to your computer and use it in GitHub Desktop.
RSpec + Capybara as Cucumber replacement.
# -*- 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
# -*- 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
# -*- 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
# -*- 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment