JJD*_*JJD 3 ruby rspec ruby-on-rails rack-test
我正在尝试按照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
Run Code Online (Sandbox Code Playgroud)
当我运行测试时,我得到以下输出......
$ 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
Run Code Online (Sandbox Code Playgroud)
这是我使用的帮助模块......
# /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
Run Code Online (Sandbox Code Playgroud)
注意:此问题类似于使用 Rspec 和 Rack::Test 测试 REST-API 响应。
小智 5
Rspec-rails 似乎忽略了type: :api描述块的参数,并将 /spec/api 中的所有规范视为请求规范(请参阅此处的请求规范一章)。也许类型参数已被弃用?我还没有找到任何相关文档..
type: :request我可以让你的例子在使用而不是使用时工作type: :api。我还删除了应用程序方法,因为默认情况下它已包含在 RequestExampleGroup 中。
# /support/api/helper.rb
module ApiHelper
include Rack::Test::Methods
end
RSpec.configure do |config|
config.include ApiHelper, type: :request
end
Run Code Online (Sandbox Code Playgroud)
和规格:
#/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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2422 次 |
| 最近记录: |