范围控制器的功能测试无法找到正确的路线

时间:2015-12-16 21:13:42

标签: ruby-on-rails ruby

我的invoice_id是:

routes.rb

我的Rails.application.routes.draw do namespace :api, path: "/", constraint: { subdomain: "api" }, defaults: { format: :json } do scope module: :v1 do resources :users, only: [:show] end end end 是:

users_controller.rb

我的测试是:

class Api::V1::UsersController < ApplicationController
  respond_to :json

  def show
    respond_with User.find(params[:id])
  end

end

为什么此测试会出现以下错误:

require 'test_helper'
class Api::V1::UsersControllerTest < ActionController::TestCase

  test '#show displays the specific user' do 
    @user = FactoryGirl.create(:user)

    get :show, id: @user.id, format: :json
    assert_response :success
  end
end

1 个答案:

答案 0 :(得分:0)

我只需要在测试中添加约束:

get :show, id: @user.id, format: :json, :constraint=>{:subdomain=>"api"}