2013-06-04 65 views
5

我正在嘗試爲名稱空間控制器創建RSpec控制器測試,但rspec似乎無法檢測到嵌套併爲post :create動作生成正確的路徑。RSpec控制器測試不生成正確的url

這是我的規格代碼:

# for: /app/controllers/admin/crm/report_adjustments_controller.rb 
require 'spec_helper' 
describe Admin::Crm::ReportAdjustmentsController do 
    render_views 

    before(:each) do 
    signin 
    end 

    describe "GET 'index'" do 
    it "returns http success" do 
     get :index 
     response.should be_success 
    end 
    end 

    describe "POST 'create'" do 
    it "creates with right parameters" do 
     expect { 
     post :create, report_adjustment: {distributor_id: @ole_distributor.id, amount: "30.0", date: Date.today } 
     }.to change(Crm::ReportAdjustment, :count).by(1) 
     response.should be_success 
    end 
    end 
end 

# routes.rb 
namespace :admin do 
    namespace :crm do 
    resources :report_adjustments 
    end 
end 

對於該代碼,get :index作品就好了,但是當post :create被調用時,會生成以下錯誤:undefined method 'crm_report_adjustment_url'

爲什麼RSpec的是足夠聰明用get :index來解決問題,但不能用post :create?我如何讓RSpec正確加載正確的路線,即admin_crm_report_adjustments_url

在此先感謝。

+1

沒有':GET'和':POST'應處理相同。看看'process'處理請求的東西:http://apidock.com/rails/v3.2.13/ActionController/TestCase/Behavior/process你可以嘗試在你的應用程序中使用pry,debugger或調試 – phoet

+0

我遇到同樣的問題,您是否找到解決方案@roflmao? – dwhite

回答

1

嘗試張貼的網址,而不是:

post admin_crm_report_adjustments_url 

# or 

post "/admin/crm/report_adjustments"