2017-01-31 27 views
1

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

所以对于我的测试,我需要指定其是否“安装”或在测试中“重新连接”。

+0

我觉得你的方式已经绘制了你的路线是有缺陷的 - 控制器应该如何知道'reconnects'和'installs'之间的区别?两者都指向相同控制器上的相同方法。 – oolong

+0

请参阅编辑我的文章以包含缺少的方法 –

+2

注意:不是答案......只是想你可能想知道。而不是“@appointment_type_singular = @ appointment_type.chomp(”s“)'我会使用'@appointment_type_singular = @ appointment_type.singularize' –

回答

1

您应该能够使用use_route帮助你RSpec的测试,以实现这一目标:

params = { 
    use_route: 'reconnects', 
    id:  @reconnect.id, 
    state:  'Finish', 
    reconnect: { notes: 'blah' } 
} 

put :update, params 
0

乌龙茶的答案是最接近的,但解决的办法是:

params = { 
    :use_route => 'reconnects', 
    :id => @reconnect.id, 
    :state => "Finish", 
    :reconnect => { 
    :notes => "blah" 
    } 
} 
put :update, params 
+0

编辑我的答案以解决您可能遇到的语法问题 – oolong