Rspec:在路由规范中添加一些头请求

时间:2012-07-11 16:14:46

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

我正在开发一个Rails应用程序,它具有JSON格式的REST API并且版本化(根据这个优秀的Ryan演员:http://railscasts.com/episodes/350-rest-api-versioning)。

例如,有一个spec / requests规范:

require 'spec_helper'

describe "My Friends" do
  describe "GET /my/friends.json" do
    it "should get my_friends_path" do
      get v1_my_friends_path, {}, {'HTTP_ACCEPT' => 'application/vnd.myapp+json; level=1'}
      response.status.should be(401)
    end
  end
end

效果很好。但是(保持这个例子)我们如何编写路由规范?例如,这个规范是不正确的:

require 'spec_helper'

describe "friends routing" do
  it "routes to #index" do
    get("/my/friends.json", nil, {'HTTP_ACCEPT' => 'application/vnd.myapp+json; level=1'}).
      should route_to({ action: "index",
                    controller: "api/v1/private/my/friends",
                        format: "json" })
  end
end

我尝试了不同的方式(例如request.headers['Accept']@request.headers['Accept'],其中request未定义且@request为零);我真的不知道该怎么做。

我使用的是Ruby 1.9.3,Rails 3.2.6和rspec-rails 2.11.0。感谢。

5 个答案:

答案 0 :(得分:13)

通过结合Cristophe和Piotr的答案,我想出了一个适合我的解决方案。我正在使用rspec和rails 3.0。

it 'should route like i want it to' do 
  Rack::MockRequest::DEFAULT_ENV["HTTP_ACCEPT"] = "*/*"
  {get: "/foo/bar"}.
    should route_to(
    controller: 'foo',
    action: 'bar',
  )
  Rack::MockRequest::DEFAULT_ENV.delete "HTTP_ACCEPT"
end

答案 1 :(得分:5)

目前您无法在路由规范中发送添加标题,这是由line 608 actionpack-3.2.5/lib/action_dispatch/routing/route_set.rb中的env = Rack::MockRequest.env_for(path, {:method => method}) 所示:

path

"/my/friends.json"是您要求的路径:get,方法是env 生成的{ "rack.version"=>[1, 1], "rack.input"=>#<StringIO:0xb908f5c>, "rack.errors"=>#<StringIO:0xb908fac>, "rack.multithread"=>true, "rack.multiprocess"=>true, "rack.run_once"=>false, "REQUEST_METHOD"=>"GET", "SERVER_NAME"=>"your-url.com", # if path was http://your-url.com/ "SERVER_PORT"=>"80", "QUERY_STRING"=>"", "PATH_INFO"=>"/", "rack.url_scheme"=>"http", "HTTPS"=>"off", "SCRIPT_NAME"=>"", "CONTENT_LENGTH"=>"0" } 包含以下内容:

Rack::MockRequest::env_for

如果你能够模拟{ get: '/' }.should route_to(controller: 'main', action: 'index') ,那么应该可以注入除env_for生成的标题之外的其他标题(参见上面的哈希)。

除了你当前正在使用route_to matcher错误之外,你应该在Hash上调用它,你可以在Hash中指定方法和路径:

{{1}}

如果您能够模拟出env_for并让它返回标题,请告诉我们。很高兴知道。

此致 克里斯托弗

答案 2 :(得分:5)

before do
  ActionDispatch::TestRequest::DEFAULT_ENV["action_dispatch.request.accepts"] = "application/vnd.application-v1+json"
end

after do
  ActionDispatch::TestRequest::DEFAULT_ENV.delete("action_dispatch.request.accepts")
end

答案 3 :(得分:2)

您可以使用rspec&#39; and_wrap_original来模拟Rack::MockRequest.env_for方法:

expect(Rack::MockRequest).to receive(:env_for).and_wrap_original do |original_method, *args, &block|
  original_method.call(*args, &block).tap { |hash| hash['HTTP_ACCEPT'] = 'application/vnd.myapp+json; level=1' }
end

答案 4 :(得分:0)

对于Rails 3和4,我在RSpec around hook中完成了以下操作:

around do |example|
  Rack::MockRequest::DEFAULT_ENV['HTTP_ACCEPT'] = 'application/vnd.vendor+json; version=1'

  example.run

  Rack::MockRequest::DEFAULT_ENV.delete 'HTTP_ACCEPT'
end

Rack >= 2.0.3 used by Rails 5)后Rack::MockRequest::DEFAULT_ENV hash is frozen

您可以重新定义常量并使用Kernel.silence_warnings来消除Ruby警告:

around do |example|
  silence_warnings do
    Rack::MockRequest::DEFAULT_ENV = Rack::MockRequest::DEFAULT_ENV.dup
  end

  Rack::MockRequest::DEFAULT_ENV['HTTP_ACCEPT'] = 'application/vnd.vendor+json; version=1'

  example.run

  Rack::MockRequest::DEFAULT_ENV.delete 'HTTP_ACCEPT'
end

这有点像黑客,但它就像一个魅力。