我有以下測試:RSpec的:Put請求定義URL源
it "should generate invoice on reconnect finish after cycle_date" do
params = {
:id => @reconnect.id,
:state => "Finish",
:install => {
:notes => "blah"
}
}
put :update, params
@reconnect.reload
expect(@reconnect.customer.invoices.count).to eq 1
expect(@reconnect.state). to eq "completed"
end
,但我需要爲線put :update, params
讓我指定的URL它是從,因爲在我的路線來:
resources :installs, :except => [:index, :show], :controller => "appointments" do
collection do
get ':id/admin_edit', to: 'appointments#admin_edit', as: :edit_admin
end
end
resources :reconnects, :except => :index, :controller => "appointments" do
collection do
get ':id/schedule_reconnect/:address_id', to: 'reconnects#schedule_reconnect', as: :schedule
get 'reconnect_appointment', to: 'reconnects#reconnect_appointment'
post 'submit_reconnect', to: 'reconnects#submit_reconnect', as: :submit_reconnect
post 'complete_reconnect', to: 'reconnects#complete_reconnect', as: :complete_reconnect
get ':id/admin_edit', to: 'appointments#admin_edit', as: :edit_admin
end
end
,並在控制器:
def classify_path
@appointment_type = self.request.path.split("/")[1]
@appointment_type_singular = @appointment_type.chomp("s")
@appointment_type_class = @appointment_type.classify.constantize
end
所以對於我的測試,我需要指定其是否「安裝」或在測試中「重新連接」。
我覺得你的方式已經繪製了你的路線是有缺陷的 - 控制器應該如何知道'reconnects'和'installs'之間的區別?兩者都指向相同控制器上的相同方法。 – oolong
請參閱編輯我的文章以包含缺少的方法 –
注意:不是答案......只是想你可能想知道。而不是「@appointment_type_singular = @ appointment_type.chomp(」s「)'我會使用'@appointment_type_singular = @ appointment_type.singularize' –