通过命名路由测试页面的行为

时间:2012-05-05 00:37:42

标签: ruby-on-rails rspec controller rspec-rails

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

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请求更改为

get workouts_date_path('2012-12-31')

然而我仍然得到

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

1 个答案:

答案 0 :(得分:1)

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