RSpec:需要帮助播种/了解Requests Specs&助手

时间:2011-05-20 23:52:19

标签: ruby-on-rails ruby-on-rails-3 rspec rspec2

我正在尝试了解RSpec的请求规范,据我所知,RSpec相当于集成测试。我似乎无法使用Race实例正确播种。好像start_dateend_date似乎nil而不是实际日期,因此在与Date.todayself.start_date <= Date.today && self.end_date >= Date.today)进行比较时会失败。

这是一个我无法通过的基本内容:

require 'spec_helper'
describe "Races" do
  describe "GET /races" do
    it "should display page" do
      Race.create!(:name => "Race Name", :start_date => Date.yesterday, :end_date => Date.tomorrow, :homepage => "http://website.com")
      get races_path
      response.status.should be(200)
      response.should render_template(:index)
    end
  end
end

此操作因此错误而失败:

Failure/Error: get races_path
     ActionView::Template::Error:
       undefined method `<=' for nil:NilClass
     # ./app/models/race.rb:5:in `current?'
     # ./app/helpers/races_helper.rb:12:in `formatted_race_dates'

应用程序/模型/ race.rb:

class Race < ActiveRecord::Base
  def current?
    self.start_date <= Date.today && self.end_date >= Date.today
  end
end

应用程序/助手/ races_helper.rb:

module RacesHelper
  def formatted_race_dates(race)
    link_to(race.homepage, :id => race.current? ? 'live-race' : nil) do
        raw("<strong>#{race.name}</strong> <em>#{race_dates_string(race)}</em>")
    end
  end
end

0 个答案:

没有答案