0

我正在尝试使用 rspec 集成(请求)测试来测试我的 API。

我在浏览器中转到我的 api 端点,0.0.0.0:3000/api/regions它返回我的数据,我得到一个会话 ID,看起来一切正常。

我在我的 API 中使用机架保护:

module Api
  class Root < Grape::API
     use Rack::Protection, instrumenter: ActiveSupport::Notifications
     use Rack::Protection::AuthenticityToken    
     prefix  'api'

     helpers do
        def warden
          request.env['warden']
        end
     end

     version 'v1', using: :header, vendor: 'vendor', strict: false do
        mount Resources::V1::Search => '/'
        mount Resources::V1::Regions => '/'
     end    

   end
end

api资源:

module Resources
  module V1
    class Regions < Grape::API

      resource :regions do
        get do
          # Some code...
        end
      end
    end
  end
end

spec_helper.rb

[...]
RSpec.configure do |config|
  config.include FactoryGirl::Syntax::Methods

  config.include Rack::Test::Methods, type: :request
  config.include Module.new { def app; Api::Root; end }, type: :request

  config.include Warden::Test::Helpers

  # ## Mock Framework
  #
  # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
  #
  # config.mock_with :mocha
  # config.mock_with :flexmock
  # config.mock_with :rr

  # 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 = true

  # If true, the base class of anonymous controllers will be inferred
  # automatically. This will be the default behavior in future versions of
  # rspec-rails.
  config.infer_base_class_for_anonymous_controllers = false

  # Run specs in random order to surface order dependencies. If you find an
  # order dependency and want to debug it, you can fix the order by providing
  # the seed, which is printed after each run.
  #     --seed 1234
  config.order = "random"
end

这是一个测试:

describe Resources::V1::Regions do
  describe 'GET /regions' do
    it 'returns some data' do
      get '/api/regions'
      [... expectations - doesn't get here...]
    end
  end
end

这是错误:

 RuntimeError:
       you need to set up a session middleware *before* Rack::Protection::RemoteToken
     # ./spec/requests/api/region_spec.rb:6:in `block (3 levels) in <top (required)>'

来自这里:https ://github.com/rkh/rack-protection/blob/master/lib/rack/protection/base.rb#L85

所以我需要添加机架会话中间件,对吗?

我添加use Rack::Session::Cookie, secret: '...'到我的 api 中,这让我 request.env['warden'] 为零(另一个问题另一个时间)。

但是,现在,当我使用浏览器加载端点时,我得到:

undefined method `each' for #<ActionDispatch::Request::Session:0x7f7bf9e521e0 not yet loaded>

在这里提出:https ://github.com/rack/rack/blob/master/lib/rack/session/abstract/id.rb#L158

我怀疑我不需要use Rack::Session::Cookie,因为服务器加载时正在执行其他操作,但我需要添加一些内容才能使测试正常工作。不知道那是什么东西。

如果您需要任何其他信息,请告诉我。

版本:

  • 葡萄(0.6.1)
  • 导轨 (4.0.2)
  • 机架保护 (1.5.2)
  • rspec (2.14.1)
4

1 回答 1

0

我通过在请求的第三个参数中添加“rack.session”哈希来解决这个问题,即get '/api/regions', {}, {'rack.session' => {}}

但我找到了一个更好的方法:https ://github.com/heavysixer/grape-on-rack/pull/1 ,它添加了会话并同时解决了守望者问题。

RSpec.configure do |config|
[...]
  rack_app = Module.new do 
    def app
      @app ||= Rack::Builder.new do
        use Rack::Session::Cookie
        Warden::Manager.serialize_into_session { |user| user.id }
        Warden::Manager.serialize_from_session { |id| User.get(id) }
        use Warden::Manager do |manager|
          manager.default_strategies :password, :basic
          # manager.failure_app = Acme::BadAuthentication
        end
        run Api::Root
      end
    end
  end

  config.include rack_app, type: :request
  end

除非有人有更好的东西,否则标记为答案。

于 2014-03-16T17:56:45.553 回答