2017-03-10 127 views
0

編寫某些控制器的測試中,使用render_views檢查一些部分呈現....Rspec的控制器測試:未定義方法 'orders_path'

describe PromoCodeController do 
    render_views 

    describe "GET 'show" do 
... a bunch of tests 

it "renders 'used' partial when promo code has already been used" do 
    @promo_code = create(:promo_code) 
    @user.stub(:promo_used?).and_return(true) 
    get 'show', :slug => @promo_code.slug 
    expect(response).to render_template(:partial => 'promo_code/_used') 
end 

它加載在_used局部

<article> 
    <p><%= @promo.description.html_safe %></p> 
    <p>Sorry, it appears this promo code has already been used. Please try again or contact us directly.</p> 
    <%= link_to "View Order", orders_path(@order), class: "box-button-black", data: { bypass: true } %> 
</article> 

但符與:

undefined method `orders_path' for #<#<Class:0x007fd4069d06e8>:0x007fd401e3e518> 

關於如何 (一)忽略了Rails的鏈接,這是風馬牛不相及的測試 (B)在測試中包括一些認識到,鏈接 (三)存根它(萬不得已,我認爲)

一切我到目前爲止已經試過沒有超過錯誤。

編輯:

orders_path錯了,應該是order_path。改變後,我得到:

ActionView::Template::Error: 
     No route matches {:controller=>"order", :action=>"show", :id=>nil} 

所以部分正在尋找@order。我試着用controller.instance_variable_set(:@order, create(:order))來設置它,但是在部分它會回到nil

通過在視圖中添加<% @order = Order.last %>進行快速測試,部分綠色通過。如何將var @order傳遞給_used部分現在是個問題。

+0

你加訂購資源到你的config/routes.rb? –

+1

它應該是order_path嗎? – margo

回答

0

首先,我需要將其更改爲order_pathorders_path是錯誤的。衛生署。

比我需要存根一些方法來解決錯誤

ActionView::Template::Error: 
     No route matches {:controller=>"order", :action=>"show", :id=>nil} 

最終,磕碰它分配一個完整的訂單到CURRENT_USER方法assign_promo_to_users_order奏效了:

it "renders 'used' partial when promo code has already been used" do 
    @promo_code = create(:promo_code) 
    @user.stub(:promo_used?).and_return(true) 
    User.any_instance.stub(:assign_promo_to_users_order).and_return(create(:order, :complete)) 
    get 'show', :slug => @promo_code.slug 
    expect(response).to render_template(:partial => 'promo_code/_used') 
end 
0

嘗試添加規範的類型。

我相信動作控制器URL幫助程序包含在規範類型中。

嘗試:

describe SomeController, type: :controller do 
0

不用手動設置參數類型,你可以設置它的基於文件的位置

# spec_helper.rb 

RSpec.configure do |config| 
    config.infer_spec_type_from_file_location! 
end 

describe 'GET SHOW' do 
    run this in a before block 
    before do 
    controller.instance_variable_set(:@order, create(:order)) 
    end 

    it "renders 'used' partial when promo code has already been used" do 
    promo_code = create(:promo_code) 
    @user.stub(:promo_used?).and_return(true) 
    # check if @order variable is assigned in the controller 
    expect(assigns(:order).to eq order 
    get 'show', slug: promo_code.slug 
    expect(response).to render_template(:partial => 'promo_code/_used') 
    end 
end 
+0

謝謝大衛......我仍然得到ActionView :: Template :: Error: 沒有路由匹配{:controller =>「order」,:action =>「show」,:id => nil}'。仍然沒有獲得@訂單的部分 –

+0

瑞安你可以複製和粘貼你的show方法在PromoCodesController? –

相關問題