加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 21:43:35 所属栏目:百科 来源:网络整理
导读:我正在使用具有 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/
我正在使用具有 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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读