在此讨论的启发问题 ,我想要写我的集成测试的登录方法。 在我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