Capybara只持有一个授权请求,其他所有请求返回401

时间:2016-12-09 18:47:14

标签: ruby-on-rails ruby ruby-on-rails-4 testing rspec

我的问题是,水豚只持有一个授权请求(无论是通过登录表单还是通过帮助器login_as方法授权) 其他(第二,第三)请求未经授权! 它不会影响我使用的驱动程序。问题出在哪里?

# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
# Prevent database truncation if the environment is production
abort('The Rails environment is running in production mode!') if Rails.env.production?
require 'spec_helper'
require 'rspec/rails'
require 'capybara/rspec'
require 'capybara/poltergeist'

# NOTE: For Chrome support
Capybara.register_driver :selenium do |app|
  Capybara::Selenium::Driver.new(app, :browser => :chrome)
end

# Capybara.register_driver(:poltergeist) { |app| Capybara::Poltergeist::Driver.new(app, js_errors: false) }
# Capybara.javascript_driver = :poltergeist

Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }

# Checks for pending migration and applies them before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.maintain_test_schema!

RSpec.configure do |config|
  config.include FactoryGirl::Syntax::Methods
  config.include WaitForAjax
  config.include FeatureHelpers
  config.include Devise::TestHelpers, type: :controller
  config.include Warden::Test::Helpers
  config.extend ControllerHelpers,    type: :controller

  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
  config.fixture_path = "#{::Rails.root}/spec/fixtures"

  # If you're not using ActiveRecord, or you'd prefer not to run each of your
  # examples within a transaction, remove the following line or assign false
  # instead of true.
  config.use_transactional_fixtures = false

  # RSpec Rails can automatically mix in different behaviours to your tests
  # based on their file location, for example enabling you to call `get` and
  # `post` in specs under `spec/controllers`.
  #
  # You can disable this behaviour by removing the line below, and instead
  # explicitly tag your specs with their type, e.g.:
  #
  #     RSpec.describe UsersController, :type => :controller do
  #       # ...
  #     end
  #
  # The different available types are documented in the features, such as in
  # https://relishapp.com/rspec/rspec-rails/docs
  config.infer_spec_type_from_file_location!

  # Filter lines from Rails gems in backtraces.
  config.filter_rails_from_backtrace!
  # arbitrary gems may also be filtered via:
  # config.filter_gems_from_backtrace("gem name")
  config.before :suite do
    Warden.test_mode!
  end
end

1 个答案:

答案 0 :(得分:0)

最有可能在第一个请求和第二个请求之间,rspec清理数据库。

对于第二个请求,用户构成第一个请求不再存在。您应该创建另一个用户并登录/授权它。

例如:

对于每个it块,您必须执行login_as(a_new_just_created_user)。是的,您可以将其放入before块中,以便为整个测试套件执行。

describe '' do
  before do
    login_as(a_new_just_created_user)
  end

  context '' do
    it '' { get 'api/url' } # first user logged in
    it '' { get 'api/url' } # second user logged in
  end

  context '' do
    it '' { get 'api/url' } # third user logged in
  end
end