我怎么能写在我的集成测试签约用户的快捷方式?(How can I write a shortcut

2019-10-24 07:47发布

在此讨论的启发问题 ,我想要写我的集成测试的登录方法。 在我test_helper.rb我甚至发现了这样的方法,但它被定义一个内部ActiveSupport::TestCase类,并从我的测试继承ActionDispatch::IntegrationTest 。 所以我复制的方法,并把它(里面test_helper.rb )到ActionDispatch::IntegrationTest类。 但它不工作,我的测试中失败,这条消息:

Capybara::ExpectationNotMet: expected "data:," to include "Study | Word Up"

它从来没有真正打开浏览器应用程序,因为它会之前。

所以我的问题是,我可以在集成测试都使用这种快捷方式,如果是,怎么样?

我与认证使用构建has_secure_password和迈克尔·哈特尔在他Railstutorial所示的机制。

这里是我的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 a 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 do |app|
  Capybara::Selenium::Driver.new(app, :browser => :chrome)
end
Capybara.current_driver = :selenium_chrome
Capybara.default_wait_time = 5 

测试本身看起来是这样的:

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

Answer 1:

你不能与水豚用#post方法,因为它不会被路由到驱动程序,这就是为什么浏览器犯规为你打开。 为了快捷方式集成测试登录,最简单的解决方法是在测试环境中这样安装后门中间件- https://robots.thoughtbot.com/faster-tests-sign-in-through-the-back-door这个例子是通关,所以你必须改变

@env[:clearance].sign_in(user)

做当用户密码被验证为有效的,但除此之外,它应该工作的罚款你正在做什么。 然后,你不会真的需要一个login_as,你可以只是做

visit page_being_tested_path(as: user.id)

注 - 您也应该有测试,做记录手动,以验证它的工作原理,以及它的很重要的,你只需要在测试环境中中间件



文章来源: How can I write a shortcut for signing a user in in my integration tests?