Created
April 9, 2014 17:36
-
-
Save skinofstars/10295355 to your computer and use it in GitHub Desktop.
RSpec + Capybara + Devise + Factory Girl: login macro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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