2

我的用户资源有以下路线,在我的规范中,我收到一条错误消息,指出该路线不存在。

更新:当我自己运行规范时,测试似乎通过了。但不是当我运行整个套件时。

规格:

describe 'POST #create' do
  let(:perform_request) do
    post :create, user: FactoryGirl.attributes_for(:user), subdomain: 'api', format: :json
  end

  it 'should be successful' do
    perform_request
    response.should be_a_success
  end

  it 'should create the user' do
    expect { perform_request }.to change { User.count }
  end

  it 'should assign the device to the user' do
    expect { perform_request }.to change { controller.send(:current_device).reload.user }
  end
end

路线:

Server::Application.routes.draw do
  scope module: :api, constraints: { subdomain: 'api' }, as: :api, defaults: { format: :json } do  
    resource :user do
      resource :emergency_contact, controller: :contacts, only: [:create, :show, :update, :destroy]
      resources :activities, only: [:create, :show, :update, :destroy, :index]
    end
    get '/*path' => 'application#invalid_url'
  end
end

错误:

1) Api::UsersController POST #create should be successful
   Failure/Error: post :create, user: FactoryGirl.attributes_for(:user), subdomain: 'api', format: :json
   ActionController::RoutingError:
   No route matches {:user=>{:first_name=>"Josh", :last_name=>"Stokes", :phone_number=>"(555) 555-1003", :password=>"qwertyuiop", :password_confirmation=>"qwertyuiop"}, :subdomain=>"api", :format=>:json, :controller=>"api/users", :action=>"create"}
   # ./spec/controllers/api/users_controller_spec.rb:10:in `block (3 levels) in <top (required)>'
   # ./spec/controllers/api/users_controller_spec.rb:14:in `block (3 levels) in <top (required)>'
4

1 回答 1

0

经过调查,似乎重新加载规范中的路线可以解决问题。

before(:all){ Rails.application.reload_routes! }
于 2013-06-24T01:31:36.093 回答