RSpec:需要帮助播种/理解请求规格和要求帮手
我试图理解 RSpec 的请求规范,据我所知,RSpec 相当于集成测试。我似乎无法使用 Race 实例正确播种它。看起来 start_date
和 end_date
是 nil
而不是实际日期,因此与 Date.today< 进行比较时它们会失败/code> (
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'
app/models/race.rb:
class Race < ActiveRecord::Base
def current?
self.start_date <= Date.today && self.end_date >= Date.today
end
end
app/helpers/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
I am trying to understand RSpec's request specs which, to the best of my understanding, are RSpec's equivalent to Integration Tests. I can't seem to seed it correctly with a Race instance. It would seem as though start_date
and end_date
are nil
instead of being actual dates so they fail when being compared to Date.today
(self.start_date <= Date.today && self.end_date >= Date.today
).
Here's a basic one that I can't get to pass:
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
this fails with this error:
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'
app/models/race.rb:
class Race < ActiveRecord::Base
def current?
self.start_date <= Date.today && self.end_date >= Date.today
end
end
app/helpers/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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论