Rails - Losing session with Integration Tests and Capybara - CSRF related?

前端 未结 4 371
执念已碎
执念已碎 2020-12-29 09:19

I\'m using Rails 3.1.0.rc4 and I\'m working on doing integration tests with capybara\'s new Steak-like DSL and Rspec (using Devise authentication)

The issue I\'m hav

4条回答
  •  自闭症患者
    2020-12-29 09:33

    The manual way of doing it is very simple:

    it "does something after login" do
      password = "secretpass"
      user = Factory(:user, :password => password)
      visit login_path
      fill_in "Email", :with => user.email
      fill_in "Password", :with => password
      click_button "Log in"
      visit # somewhere else and do something
    end
    

    You can then break this out into a function in your 'spec_helper.rb':

    # at the bottom of 'spec_helper.rb'
    def make_user_and_login
      password = "secretpass"
      @user = Factory(:user, :password => password)
      visit login_path
      fill_in "Email", :with => @user.email
      fill_in "Password", :with => password
      click_button "Log in"
    end
    

    and use it in any of your tests (probably request specs):

    it "does something after login" do
      make_user_and_login
      # now test something that requires a logged in user
      # you have access to the @user instance variable
    end
    

提交回复
热议问题