【问题标题】:Rspec: ActionView::Template::Error: undefined method 'date' for ItemRspec:ActionView::Template::Error: 项目的未定义方法“日期”
【发布时间】:2014-04-02 19:52:33
【问题描述】:

这让我在我的 Rails 4 应用程序中通过 Rspec 测试感到烦恼。

之前,我测试了一个 Rspec 功能,通过一个表单和一个布尔值 :all_or_nothing 创建一个项目。它通过了。所以要求发生了变化,如果一个项目需要DateTime 参数,我需要标记。使用 TDD,我让测试提示我何时将此功能添加到应用程序。那是我在主题行中收到错误的时候。啊,我必须进行迁移才能将这两个字段添加到 Item。完毕。但我仍然收到此错误。

嗯。 :all_or_nothing 的表单元素工作正常。我对它进行了精确建模,我想并且相信我仔细检查过在simple_form_for 的两个新字段中发现了任何打字错误。我什至在form_for 中重新编写了它,它仍然会产生相同的错误。如果我拉出:date:time,它在Item 中找不到该列。两者兼而有之,测试顺利通过,但规范失败。

作为现实检查,我可以通过网页执行此操作,并且没有发现错误,并且表单按预期工作。所以 Rspec 在 Item 中看不到 :date:time 字段。我试图帮助 Rspec 在:css 中找到它,但效果不佳。我已重新运行迁移,但如果我尚未运行迁移,则通过浏览器进行的冒烟测试将无法正常工作。

谷歌搜索这条消息并没有指出我需要的线索。 Rspec 的错误是,在此表单中,该项目在能够打开表单之前有一个未定义的方法“日期”。我不明白为什么它在模型中看不到它。我尝试了其他更改标签的组合,但仍然出现相同的错误。帮我看看我看不到的东西。

spec/feature/item/item_create_spec.rb 第 32、26 行

 require 'spec_helper'

feature "create items" do 

 background do
  @attr = {
    :name => "Cashiers are physically counting?",
    :category => "Cash",
    :sub_category => "Video Review",
    :explanation => "Floors and aisles are clear",
    :scoring => 7,
    :high_score => 10,
    :all_or_nothing => true,
    :date => "1/1/2011",
    :time => "4:59 PM"
  }

        context "in inspections" do
        background do
          @inspection = FactoryGirl.create(:inspection)
        end

        scenario "can create an item" do
          visit root_path
          expect{
             click_link "New Item"
            fill_in 'High score', with: @attr[:high_score]

            check 'All or nothing', :checked
 #          check 'Date?', :checked
            within(:css, "#item_date") do 
              check 'Date?', :checked
            end
            check 'Time?', :checked
            save_and_open_page
            click_button 'Create item'
          }.to change(Item, :count).by(1)
        end

   end

 end

_form.html.erb 第 9、10 行

 <%= simple_form_for @item do |f| %>
 <%= f.input :category %>
 <%= f.input :name %>
 <%= f.input :sub_category %>
 <%= f.input :explanation %>
 <%= f.input :scoring %>
 <%= f.input :high_score %>
 <%= f.input :all_or_nothing, as: :boolean, checked_value: true, unchecked_value: false %>
 <%= f.input :date, as: :boolean, checked_value: true, unchecked_value: false %>
 <%= f.input :time, as: :boolean, checked_value: true, unchecked_value: false  %>
 <%= f.button :submit %>
<% end %>

schema.rb 的相关部分

  create_table "items", force: true do |t|
  t.string   "category"
  t.string   "sub_category"
  t.string   "name"
  t.string   "explanation"
  t.integer  "scoring"
  t.integer  "high_score"
  t.boolean  "all_or_nothing"
  t.datetime "created_at"
  t.datetime "updated_at"
  t.integer  "survey_id"
  t.boolean  "date"
  t.boolean  "time"
 end

add_date_to_items.rb

 class AddDateToItems < ActiveRecord::Migration
   def change
     add_column :items, :date, :boolean
     add_column :items, :time, :boolean
   end
 end 

html输出的sn-p

<div class="input boolean optional item_date">
 <input name="item[date]" type="hidden" value="false">
 <label class="boolean optional control-label checkbox" for="item_date">
 <input class="boolean optional" id="item_date" name="item[date]" type="checkbox" value="true">
    Date</label>
  </div>

如果更清楚,这里有一个要点:

https://gist.github.com/sam452/9942221

【问题讨论】:

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


    【解决方案1】:

    据我了解您的问题,您在items 表中创建了两个新字段datetime。成功运行migration 并且在development 环境中一切正常。

    但是test 环境并非如此。您的测试因错误 undefined method 'date' for Item 而失败。

    这只是让我得出结论,您已经在 development 环境中运行了迁移,但没有在 test 环境中运行。

    我建议您使用运行以下命令在 test 环境中执行任何待处理的迁移:

    rake db:migrate RAILS_ENV=test
    

    在此之后运行示例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-11
      • 1970-01-01
      • 1970-01-01
      • 2013-04-10
      • 2015-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多