Skip to content

Instantly share code, notes, and snippets.

@skinofstars
Created April 9, 2014 17:36
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 skinofstars/10295355 to your computer and use it in GitHub Desktop.
Save skinofstars/10295355 to your computer and use it in GitHub Desktop.
RSpec + Capybara + Devise + Factory Girl: login macro
# support/feature_macros.rb
module FeaturesMacros
def signin_admin
DatabaseCleaner.clean
@user ||= FactoryGirl.create(:user)
visit '/sign_in'
within("#new_user") do
fill_in 'user[email]', :with => user.email
fill_in 'user[password]', :with => 'password1'
end
click_button 'Sign in'
expect(page).to have_content 'Signed in'
end
end
# features/items_spec.rb
require 'spec_helper'
describe "items" do
describe "Items when signed in", :type => :feature do
before(:each) do
DatabaseCleaner.clean
20.times { |i| FactoryGirl.create(:item) }
signin_user
end
describe "GET /" do
it "can GET /" do
visit asset_groups_path
page.status_code.should == 200
expect(page).to have_content 'My items'
expect(page).to have_content 'Other stuff'
end
end
end
end
# spec_helper.rb
#... stuff
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
Dir[Rails.root.join("spec/factories/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
#... stuff
config.include FeaturesMacros, type: :feature
end
# factories/user.rb
FactoryGirl.define do
factory :user do
sequence(:email) {|n| "user#{n}@example.com"}
password "password1"
password_confirmation "password1"
# other devise related stuff, like confirmed_at
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment