如何使rspec加载config.ru?

时间:2019-06-14 23:05:12

标签: rspec rack-contrib

我正在使用rack-contrib gem从HTTP_ACCEPT_LANGUAGE标头设置区域设置,因此我的config.ru文件具有

puts 'Running config.ru'
require 'rack'
require 'rack/contrib/locale'
use  Rack::Locale

在开发模式下运行时,我看到消息Running config.ru,但是在运行rspec时,我看不到该消息,因此rspec没有加载config.ru。

在开发模式下,按预期由Rack::Locale设置语言环境。当我运行此测试文件时,rspec spec/requests/localization_spec.rb

require 'rails_helper'
describe 'Localization' do
    describe 'Setting from header' do
        it 'should set the locale for french' do
            header 'ACCEPT_LANGUAGE', 'fr'
            gets '/'
            expect(last_request.env['rack.locale']).to eq 'fr'
        end
    end
end

语言环境未设置为:fr,并且看不到Running config.ru消息。

1 个答案:

答案 0 :(得分:1)

RSpec不加载config.ru,因为它不知道它是什么。您需要模拟Web服务器的功能。这近似于机架测试样式设置。将Rack::Test::API包含在您的规格中将使您可以对机架文件使用机架测试。

module Rack::Test::API
  include Rack::Test::Methods

  def app
    @app ||= Rack::Builder.parse_file('path/to/config.ru').first
  end
end
相关问题