Rspec失敗,ActionController::UrlGenerationError
帶有一個我認爲有效的URL。我已經嘗試了Rspec請求的參數,以及與routes.rb混帳,但我仍然失去了一些東西。Rspec失敗,出現ActionController :: UrlGenerationError
奇怪的是,當使用curl進行本地測試時,它可以100%的工作。
錯誤:
Failure/Error: get :index, {username: @user.username}
ActionController::UrlGenerationError:
No route matches {:action=>"index", :controller=>"api/v1/users/devices", :username=>"isac_mayer"}
相關的代碼:
規格/ API/V1 /用戶/ devices_controller_spec.rb
require 'rails_helper'
RSpec.describe Api::V1::Users::DevicesController, type: :controller do
before do
@user = FactoryGirl::create :user
@device = FactoryGirl::create :device
@user.devices << @device
@user.save!
end
describe "GET" do
it "should GET a list of devices of a specific user" do
get :index, {username: @user.username} # <= Fails here, regardless of params. (Using FriendlyId by the way)
# expect..
end
end
end
應用程序/控制器/ API /v1/users/devices_controller.rb
class Api::V1::Users::DevicesController < Api::ApiController
respond_to :json
before_action :authenticate, :check_user_approved_developer
def index
respond_with @user.devices.select(:id, :name)
end
end
的config/routes.rb中
namespace :api, path: '', constraints: {subdomain: 'api'}, defaults: {format: 'json'} do
namespace :v1 do
resources :checkins, only: [:create]
resources :users do
resources :approvals, only: [:create], module: :users
resources :devices, only: [:index, :show], module: :users
end
end
end
相關線路從rake routes
api_v1_user_devices GET /v1/users/:user_id/devices(.:format) api/v1/users/devices#index {:format=>"json", :subdomain=>"api"}
你是明星。其中一件事非常明顯,但我認爲這不是問題!結束使用'get:index,user_id:@ user.username' – Dan