2012-01-12 91 views
0

我试图写测试用例嵌套的资源,我看到下面的错误匹配:的Rails的ActionController :: TestCase的无路由自定义命名的嵌套资源

Error: 
    test_should_get_single_budget_link(BudgetlinkControllerTest): 
    ActionController::RoutingError: No route matches {:budget_id=>980190962, :action=>"show", :controller=>"budgetlink"} 
    test/functional/budgetlink_controller_test.rb:5:in `test_should_get_single_budget_link' 

我的routes.rb样子:

Simplebudget::Application.routes.draw do 

root :to => "home#index" 

resources :budgets do 
    resources :transactions 
    resources :budgets, :controller => :budgetlink 
end 

resources :classifications 
resources :periodicities 

end 

我的测试如下所示:

test "should get single budget link" do 
    get :show, 'budget_id' => budgets(:one).id 
    assert_response :success 
    assert_equal assigns(:budgetlink).primary, "Budget1" 
    assert_equal assigns(:budgetlink).secondary, "Budget2" 
end 

我相信这是事实,我有ç为了使用资源名称“预算”而不是budgetlinks,Rails在我目前的测试中没有办法识别预算路线。我如何配置我的测试来识别我的重映射路线?

不知道是否有必要,但认为它不会伤害。这里是控制器代码:

class BudgetlinkController < ApplicationController 
skip_before_filter :verify_authenticity_token 

def show 
    @budgetlink = Budgetlink.find(params[:primary], params[:secondary]) 

    respond_to do |format| 
     format.html #index.html.erb 
     format.json { render :json => @budgetlink} 
    end 
end 
end 

回答

1

我确定了这个问题。我在正确的轨道上,我没有写这样的测试,它会产生一个GET到正确的资源/路线。

test "should create budget link" do 
    assert_difference('Budgetlink.count') do 
     post :create, :budget_id => budgets(:one).id, 
        :budgetlink => { :primary => budgets(:one).id, 
             :secondary => budgets(:two).id} 
    end  

    assert_redirected_to budget_path(assigns(:primarybudget)) 
    assert_equal 'Budget link created.', flash[:success] 
end