子域约束引发规范中找不到的路径

时间:2013-06-23 21:53:01

标签: ruby-on-rails rspec

我的用户资源有以下路由,在我的规范中,我收到一条错误消息,说明路由不存在。

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

规格:

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)>'

1 个答案:

答案 0 :(得分:0)

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

before(:all){ Rails.application.reload_routes! }