RSpec:需要帮助播种/理解请求规格和要求帮手

发布于 2024-11-08 19:00:23 字数 1500 浏览 0 评论 0原文

我试图理解 RSpec 的请求规范,据我所知,RSpec 相当于集成测试。我似乎无法使用 Race 实例正确播种它。看起来 start_dateend_datenil 而不是实际日期,因此与 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文