ruby-on-rails – Rspec:在路由规范中添加一些头部请求

我正在使用具有 JSON格式的REST API的Rails应用程序,并且版本(根据这个优秀的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为nil);我真的看不到怎么办

我在Ruby 1.9.3,Rails 3.2.6和rspec-rails 2.11.0.谢谢.

解决方法

通过结合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

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...