机架测试失败:JSON 请求尚无响应 [英] Rack test fails: No response yet for JSON request
问题描述
我正在尝试按照 Yehuda 中提供的 Ticketee 示例为我的 Ruby 项目创建 JSON APIKatz 的书 Rails 3 in Action,第 13 章.这是第 353 页描述的 RSpec 测试,适用于我的环境.
I am trying to create a JSON API for my Ruby project following the Ticketee example provided in Yehuda Katz's book Rails 3 in Action, chapter 13. Here is an RSpec test described on page 353 adapted to my environment.
# /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
当我运行测试时,我得到以下输出......
When I run the test I get the following output ...
$ 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
这是我使用的辅助模块...
Here is the helper module I use ...
# /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
注意:这个问题类似于使用 Rspec 和 Rack::Test 测试 REST-API 响应.
推荐答案
Rspec-rails 似乎忽略了 describe 块的 type: :api
参数并将/spec/api 中的所有规范视为请求规范(请参阅此处的请求规范一章).也许不推荐使用类型参数?我还没有找到它的任何文档..
Rspec-rails seems to ignore the type: :api
parameter of the describe block and treats all specs in /spec/api as request specs (see chapter request specs here). Maybe the type-parameter is deprecated? I haven't found any documentation for it yet..
当使用 type: :request
而不是 type: :api
时,我可以让你的示例工作.我还删除了 app-method,因为它默认已包含在 RequestExampleGroup 中.
I can get your example to work when using type: :request
instead of type: :api
. I also removed the app-method as it's already included in the RequestExampleGroup by default.
# /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
这篇关于机架测试失败:JSON 请求尚无响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!