我有以下测试: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' –