ruby on rails - How can I write a shortcut for signing a user in in my integration tests? -


motivated discussion in question, want write log-in method integration tests. in test_helper.rb found such method, defined inside activesupport::testcase class, , test inherits actiondispatch::integrationtest. copied method , put (inside test_helper.rb) actiondispatch::integrationtest class. doesn't work , tests fail message:

capybara::expectationnotmet: expected "data:," include "study | word up" 

it never opens app in browser before.

so question is, can use such shortcut @ in integration tests, , if yes, how?

i using build in authentication has_secure_password , mechanism shown michael hartl in railstutorial.

here test_helper.rb:

env['rails_env'] ||= 'test' require file.expand_path('../../config/environment', __file__) require 'rails/test_help' require 'minitest/rails' require 'minitest/rails/capybara' require 'capybara/rails' require 'capybara/poltergeist'  class activesupport::testcase   activerecord::migration.check_pending!   fixtures :all   # logs in test user.   def log_in_as(user, options = {})     password    = options[:password]    || 'password'     remember_me = options[:remember_me] || '1'     if integration_test?      post login_path, session: { email:       user.email,                               password:    password,                               remember_me: remember_me }     else       session[:user_id] = user.id     end   end    private    def integration_test?     defined?(post_via_redirect)   end end  class actiondispatch::integrationtest   include capybara::dsl    def log_in_as(user, options = {})     password    = options[:password]    || 'password'     remember_me = options[:remember_me] || '1'     if integration_test?       post login_path, session: { email:       user.email,                               password:    password,                               remember_me: remember_me }     else       session[:user_id] = user.id     end   end end  class activerecord::base     mattr_accessor :shared_connection   @@shared_connection = nil    def self.connection    @@shared_connection || retrieve_connection   end end    activerecord::base.shared_connection = activerecord::base.connection   capybara.register_driver :selenium_chrome |app|   capybara::selenium::driver.new(app, :browser => :chrome) end capybara.current_driver = :selenium_chrome capybara.default_wait_time = 5  

the test looks this:

require 'test_helper'  class studycapybaratest < actiondispatch::integrationtest   def setup     @user = users(:archer)     @vocabs = @user.vocabs      log_in_as @user      # visit login_path     # fill_in "session_email",  with: @user.email     # fill_in "session_password",   with: 'password'     # click_button "session_commit"   end   ....  end 

you can't use #post method capybara since won't routed driver, why browser doesnt open you. shortcut login in integration tests, easiest solution install backdoor middleware in test environment - https://robots.thoughtbot.com/faster-tests-sign-in-through-the-back-door example clearance you'll have change

@env[:clearance].sign_in(user) 

to whatever you're doing when users password verified valid, other should work fine. wouldn't need login_as, do

visit page_being_tested_path(as: user.id) 

note -- should have tests logging in manually verify works, , important have middleware in test environment


Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -