我正在获取一个ActionController :: UrlGenerationError:当我尝试测试目标控制器时,没有路由匹配(:action =>“edit”,:controller =>“目标”)错误
这是我的goals_controller_test.rb
require 'test_helper'
class GoalsControllerTest < ActionController::TestCase
test "should be redirected when not logged in" do
get :new
assert_response :redirect
assert_redirected_to new_user_session_path
end
test "should render the new page when logged in" do
sign_in users(:guillermo)
get :new
assert_response :success
end
test "should get edit" do
get :edit
assert_response :success
end
test "should get show" do
get :show
assert_response :success
end
end
这是我的 routes.rb
Rails.application.routes.draw do
devise_for :users
authenticated :user do
root 'du#dashboard', as: "authenticated_root"
end
resources :goals
root 'du#Home'
end
我的 goals_controller.rb
class GoalsController < ApplicationController
before_filter :authenticate_user!, only: [:new]
def new
end
def edit
end
def show
end
private
def find_user
@user = User.find(params[:user_id])
end
def find_goal
@goal = Goal.find(params[:id])
end
end
我觉得奇怪的是,如果我使用获取'目标/编辑'而不是资源:目标,则测试通过。
非常感谢您的任何指导。
答案 0 :(得分:9)
使用 goals GET /goals(.:format) goals#index
POST /goals(.:format) goals#create
new_goal GET /goals/new(.:format) goals#new
edit_goal GET /goals/:id/edit(.:format) goals#edit
goal GET /goals/:id(.:format) goals#show
PATCH /goals/:id(.:format) goals#update
PUT /goals/:id(.:format) goals#update
DELETE /goals/:id(.:format) goals#destroy
时,Rails会为您生成以下路由(RESTful):
edit
如您所见,要点击/goals/:id/edit
操作:id
,您需要传递:id
。这样,在您的控制器中,您将能够通过给定Goal.find(params[:id])
=&gt;找到记录。 :id
。因此,在测试中,您需要传递此get :edit, id: 1 # mapping to /goals/1/edit
,例如:
get 'goals/edit'
如果您手动添加此路线/goals/edit
,它会起作用,因为它直接映射到:id
(注意没有{{1}})。
顺便说一句,我建议您查看官方路线指南:http://guides.rubyonrails.org/routing.html
答案 1 :(得分:3)
@goal = Goal.create(your params here) or use factory girl gem or fixtures
您应该传递ID get :edit ,id: @goal