Log-in through authlogic without having to fill in form every time

£可爱£侵袭症+ 提交于 2019-12-07 01:15:39

问题


I have a number of Cucumber scenarios which run with capybara on a project I am working on.

Most of these scenarios start with a "Given I am logged in" step. Currently my implementation of this is:

  visit path_to('the login page')
  fill_in('Username', :with => 'user')
  fill_in('Password', :with => 'password')
  click_button('Login')

This works fine, however it is becoming a bit time consuming having to load and submit the login form before every single scenario.

Is there a way to simply set up the session in this step without having to go through the form every single time?


回答1:


A bit late to the show as usual but this works for me on Rails 3.0.10.

In features/support/authlogic.rb:

require "authlogic/test_case"
World(Authlogic::TestCase)
ApplicationController.skip_before_filter :activate_authlogic
Before do
  activate_authlogic
end

Then in features/step_definitions/user_sessions_steps.rb

Given /^I am already logged in$/ do
  UserSession.create!(User.find_by_name!('user'))
end

Obviously, you can pass a username into the step definition if you want to login a specific user.

Full details are in this blog post: http://laserlemon.com/blog/2011/05/20/make-authlogic-and-cucumber-play-nice/




回答2:


you can use Background in cucumber..ie

Background:
    Given I am a logged-in admin user

that will DRY up your scenarios.

http://github.com/aslakhellesoy/cucumber/wiki/background




回答3:


Borrowing a trick from another auth gem Sorcery's wiki: Integration Testing with Rspec, Capybara and Fabricator, they use:

page.driver.post(user_sessions_url, { username: user, password: password })

Which basically calls the user_sessions_controller#create method, so make sure the args match up. So for me, I did:

activate_authlogic
page.driver.post(user_sessions_url, { user_session: { email: user.email, password: user.password } })

I was searching for this for a few days. and UserSession.create! hadn't been able to work for me. Although this is an old question, I'm on Rails 4, hopefully it may help others stuck too.



来源:https://stackoverflow.com/questions/3629139/log-in-through-authlogic-without-having-to-fill-in-form-every-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!