rspec:put request定义url源

时间:2017-01-31 23:33:32

标签: ruby-on-rails rspec

我有以下测试:

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行允许我指定它来自哪个网址,因为在我的路线中:

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

因此,对于我的测试,我需要在测试中指定它是“安装”还是“重新连接”。

2 个答案:

答案 0 :(得分:1)

您应该能够在rspec测试中使用use_route助手来实现这一目标:

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

put :update, params

答案 1 :(得分:0)

oolong的答案是最接近的,但解决方案是:

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