Log-in through authlogic without having to fill in

2019-04-25 06:17发布

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?

3条回答
甜甜的少女心
2楼-- · 2019-04-25 06:40

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.

查看更多
Root(大扎)
3楼-- · 2019-04-25 06:41

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/

查看更多
手持菜刀,她持情操
4楼-- · 2019-04-25 06:46

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

查看更多
登录 后发表回答