如何在集成测试中编写用于签署用户的快捷方式? [英] How can I write a shortcut for signing a user in in my integration tests?

查看:77
本文介绍了如何在集成测试中编写用于签署用户的快捷方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

受此问题中的讨论的激励,我想写一个我的集成测试的登录方法.在我的test_helper.rb中,我什至找到了这样的方法,但是它是在ActiveSupport::TestCase类中定义的,并且我的测试继承自ActionDispatch::IntegrationTest.因此,我复制了该方法并将其(在test_helper.rb内部)放入ActionDispatch::IntegrationTest类.但这不起作用,并且我的测试失败并显示以下消息:

Motivated by the discussion in this question, I want to write a log-in method for my integration tests. In my test_helper.rb I even found such a method, but it is defined inside a ActiveSupport::TestCase class, and my test inherits from ActionDispatch::IntegrationTest. So I copied the method and put it (inside test_helper.rb) into the ActionDispatch::IntegrationTest class. But it doesn't work and my tests fail with this message:

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

它从来没有像以前那样真正在浏览器中打开应用程序.

It never actually opens the app in the browser as it would before.

所以我的问题是,我可以在集成测试中完全使用这样的快捷方式吗?如果可以,怎么做?

So my question is, can I use such a shortcut at all in integration tests, and if yes, how?

我正在使用has_secure_password的内置身份验证以及Michael Hartl在他的Railstutorial中展示的机制.

I am using the build in authentication with has_secure_password and the mechanism shown by Michael Hartl in his 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

推荐答案

您不能在Capybara中使用#post方法,因为它不会被路由到驱动程序,这就是为什么浏览器无法为您打开的原因.要在集成测试中以快捷方式登录,最简单的解决方案是在这样的测试环境中安装后门中间件-

You can't use the #post method with Capybara since it won't get routed to the driver, which is why the browser doesnt open for you. To shortcut login in integration tests, the easiest solution is to install backdoor middleware in the test environment like this - https://robots.thoughtbot.com/faster-tests-sign-in-through-the-back-door That example is for clearance so you'll have to change the

@env[:clearance].sign_in(user)

在验证用户密码有效时可以执行任何操作,但除此之外应该可以正常工作.然后,您实际上并不需要login_as,您只需

to do whatever you're doing when a users password is verified as valid, but other than that it should work fine. Then you wouldn't actually need a login_as, you could just do

visit page_being_tested_path(as: user.id)

注意-您还应该具有手动登录的测试以验证其是否有效,并且非常重要,您仅在测试环境中拥有该中间件

Note -- you should also have tests that do the logging in manually to verify that it works, and its VERY important you only have that middleware in the test environment

这篇关于如何在集成测试中编写用于签署用户的快捷方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆