2017-01-31 23 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