【问题标题】:Rack test fails: No response yet for JSON request机架测试失败:JSON 请求还没有响应
【发布时间】:2012-12-21 18:29:21
【问题描述】:

我正在尝试按照 Yehuda Katz 的书 Rails 3 in Action 第 13 章中提供的 Ticketee 示例为我的 Ruby 项目创建 JSON API。这是第 353 页中描述的适用于我的环境的 RSpec 测试。

# /spec/api/v1/farms_spec.rb # Reduced to the minimum code.
require "spec_helper"
include ApiHelper # not sure if I need this

describe "/api/v1/farms", type: :api do
    context "farms viewable by this user" do
        let(:url) { "/api/v1/farms" }
        it "json" do
            get "#{url}.json"
            assert last_response.ok?
        end
    end
end

当我运行测试时,我得到以下输出...

$ rspec spec/api/v1/farms_spec.rb
No DRb server is running. Running in local process instead ...
Run options: include {:focus=>true}

All examples were filtered out; ignoring {:focus=>true}
  1) /api/v1/farms farms viewable by this user json
     Failure/Error: assert last_response.ok?
     Rack::Test::Error:
       No response yet. Request a page first.
     # ./spec/api/v1/farms_spec.rb:30:in `block (3 levels) in <top (required)>'

Finished in 2.29 seconds
1 example, 1 failure

这是我使用的辅助模块...

# /support/api/helper.rb
module ApiHelper
    include Rack::Test::Methods

    def app
        Rails.application
    end
end

RSpec.configure do |config|
    config.include ApiHelper, type: :api
end

注意:这个问题类似于Testing REST-API responses with Rspec and Rack::Test

【问题讨论】:

  • 你能发布你的 Gemfile 吗?
  • 这里是项目的Gemfile

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


【解决方案1】:

Rspec-rails 似乎忽略了 describe 块的 type: :api 参数,并将 /spec/api 中的所有规范视为请求规范 (see chapter request specs here)。也许类型参数已被弃用?我还没有找到它的任何文档..

当使用 type: :request 而不是 type: :api 时,我可以让您的示例正常工作。我还删除了 app-method,因为它已默认包含在 RequestExampleGroup 中。

# /support/api/helper.rb
module ApiHelper
    include Rack::Test::Methods
end

RSpec.configure do |config|
    config.include ApiHelper, type: :request
end

以及规格:

#/spec/api/v1/farms_spec.rb 
require "spec_helper"

describe "/api/v1/farms" do
    context "farms viewable by this user" do
        let(:url) { "/api/v1/farms" }
        it "json" do
            get "#{url}.json"
            assert last_response.ok?
        end
    end
end

【讨论】:

  • 完全奇怪。在其他人提出更合适的解决方案之前,我会坚持使用您的解决方法。
  • /support/api/helper.rb 在哪里? :(
猜你喜欢
  • 2015-09-10
  • 2010-12-09
  • 1970-01-01
  • 2016-10-02
  • 1970-01-01
  • 2019-08-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-01
相关资源
最近更新 更多