路径似乎存在于rake路由中,在运行spec时不可用

时间:2014-08-19 23:08:03

标签: ruby-on-rails rspec rails-routing

我似乎无法得到以下结论。

规格:(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

Controller(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

Rake Routes:

     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"}

1 个答案:

答案 0 :(得分:1)

get api_power_ups_path

这不是如何在控制器规范中使用get

在一个控制器规范中,你假设控制器中的测试类......所以你使用get来调用控制器上的实际方法。

在这种情况下,该方法称为index(即,您有def index),因此要激活测试,只需调用:

get :index

只有在引用其他路径时才使用路径助手 - 例如,您将被重定向到其他路径。