我正在尝试为嵌套资源编写测试用例,我看到以下错误:
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
我相信我已经将路由配置为使用资源名称“预算”而不是预算链接,而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
答案 0 :(得分: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