0

我有一个命名路线,用于将索引操作缩小到特定的一周

match "workouts/week/(:date)" => "workouts#index", 
  :constraints => { :date => /\d{4}-\d{2}-\d{2}/ },
  :as => "workouts_date"

我正在尝试编写一个规范来测试索引视图是否正确显示给定的几周锻炼。

  it 'populates an array of workouts for the week of the given date' do
    sunday_workout = FactoryGirl.create(:workout, date: Date.new(2012, 12, 31).beginning_of_week(:sunday))
    today_workout = FactoryGirl.create(:workout, date: Date.new(2012, 12, 31))
    saturday_workout = FactoryGirl.create(:workout, date: Date.new(2012, 12, 31).end_of_week(:sunday))
    next_week_workout = FactoryGirl.create(:workout, date: Date.new(2012, 12, 31).next_week)
    prev_week_workout = FactoryGirl.create(:workout, date: Date.new(2012, 12, 31).prev_week)
    this_week_workouts = [sunday_workout, today_workout, saturday_workout].group_by(&:date)
    get 'workouts/week/2012-12-31'
    assigns(:workouts).should eq this_weeks_workouts
  end

我得到错误:

Failure/Error: get 'workouts/week/2012-12-31'
 AbstractController::ActionNotFound:
   The action 'workouts/week/2012-12-31' could not be found for WorkoutsController

:index动作是索引,但除了提供符号之外,我不知道如何让它路由到那里。

编辑:我在 spec_helper 文件中添加了以下内容:

config.include Rails.application.routes.url_helpers

并将获取请求更改为

get workouts_date_path('2012-12-31')

但是我仍然得到

 The action '/workouts/week/2012-12-31' could not be found for WorkoutsController
4

1 回答 1

1

在控制器测试中,您应该像这样调用您的控制器:get 'index', date: '2012-12-31'. 您正在测试控制器操作,它对路由一无所知。说得通?如果您想测试路由,则有路由规范。

于 2012-05-05T10:54:50.013 回答