我正在尝试了解RSpec的请求规范,据我所知,RSpec相当于集成测试。我似乎无法使用Race实例正确播种。好像start_date
和end_date
似乎nil
而不是实际日期,因此在与Date.today
(self.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