【发布时间】:2021-07-28 21:27:22
【问题描述】:
我正在为我的 API 控制器编写规范(使用 RSpec)。当我测试索引的 GET 请求时一切正常,它返回预期的正文和成功的响应,但是,当我测试 show 操作的 GET 请求时,它会引发以下错误:
ActionController::UrlGenerationError:
No route matches {:action=>"show", :controller=>"api/v1/contacts"}
这是我在规范文件中编写的代码:
require 'rails_helper'
RSpec.describe Api::V1::ContactsController do
describe "GET #index" do
context 'with a successful request' do
before do
get :index
end
it "returns http success" do
expect(response).to have_http_status(:success)
expect(response.status).to eq(200)
end
it "JSON body response contains expected recipe attributes" do
json_response = JSON.parse(response.body)
expect(json_response.keys).to match_array(%w[data included])
end
end
end
describe "GET #show" do
context 'with a successful request' do
before do
get :show
end
it "returns http success" do
expect(response).to have_http_status(:success)
expect(response.status).to eq(200)
end
end
end
end
谁能帮我看看我错过了什么? 如何使用 RSpec 测试 show 操作的 GET 请求?
【问题讨论】:
标签: ruby-on-rails rspec