-
-
Save nicksterious/28c191cf7ec03eefe5b09af35f4d89e5 to your computer and use it in GitHub Desktop.
RSpec + Capybara + Devise + Factory Girl: login macro
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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