【发布时间】:2014-08-19 23:08:02
【问题描述】:
我似乎无法解决以下问题。
规格:(spec/api/power_ups_spec.rb)
describe Api::PowerUpsController, :type => :controller do
describe "GET power_ups" do
it "returns all power-ups" do
FactoryGirl.create :power_up, name: "Increase rate of take", description: "You gain points more quickly"
FactoryGirl.create :power_up, name: "Decrease rate of give", description: "You lose points more slowly"
get api_power_ups_path, {}, { "Accept" => "application/json" }
expect(response.status).to eq 200
body = JSON.parse(response.body)
power_up_names = body.map { |m| m["title"] }
expect(power_up_names).to match_array(["Increase rate of take",
"Decrease rate of give"])
end
end
end
路线:
Rails.application.routes.draw do
namespace :api do
resources :power_ups, only: [:index]
end
end
控制器(app/controllers/api/power_up_controller.rb):
module Api
class PowerUpsController < ApplicationController
include ActionController::MimeResponds
respond_to :json
def index
respond_with PowerUp.all
end
end
end
耙子路线:
Prefix Verb URI Pattern Controller#Action
api_power_ups GET /api/power_ups(.:format) api/power_ups#index
运行规范的错误消息:
Failure/Error: get api_power_ups_path, {}, { "Accept" => "application/json" }
ActionController::UrlGenerationError:
No route matches {:action=>"/api/power_ups", :controller=>"api/power_ups"}
【问题讨论】:
-
换成
get api_power_ups_path, :format => :json是否找到路由? -
@infused,结果与错误信息
No route matches {:action=>"/api/power_ups", :controller=>"api/power_ups", :format=>:json}中添加format=>json的结果一样@ -
我假设该路线在 RSpec 之外有效。有什么理由不在规范中使用
get :index, :format => :json? -
@infused,谢谢你的工作。不是这样的唯一原因是我在教程中看到了它。
标签: ruby-on-rails rspec rails-routing