Question

This is bugging me in a formerly passing Rspec test in my Rails 4 app.

Earlier, I had tested a Rspec feature to create an Item through a form and a boolean, :all_or_nothing. It passed. So the requirements changed that I needed to flag if an Item needed a Date and Time parameter. Using TDD, I let the test prompt me when to add this feature to the app. And that's when I got the error in the subject line. Ahh, I must make a migration to add these two fields to Item. Done. But I still get this error.

Hmm. The form element for :all_or_nothing works fine. I modeled it exactly, I thought and believe I carefully examined to find any typing goofs in the two new fields in simple_form_for. I've even re-written it in form_for and it still yields the same error. If I yank out either :date or :time, it can't find that column in Item. Yanking both, the test passes fine, but the spec is a failure.

As a reality check, I can do this through the web page and find no error and the form works as expected. So Rspec is not seeing the :date or :time field in Item. I've tried to help Rspec find it within :css but that didn't work as well. I've re-run the migration but the smoke test through the browser wouldn't work if I hadn't already run the migration.

Googling for this message didn't point me to the clue I needed. The error from Rspec is that within this form there is an undefined method 'date' for this Item before it is able to open the form. I can't see why it doesn't see it in the model. I've tried other combinations of changing the label but still the same error. Help me see what I'm unable to see.

spec/feature/item/item_create_spec.rb lines 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 lines 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 %>

relevant portion of 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 

snippet of html output

<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>

There is a gist here if that's clearer:

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

Was it helpful?

Solution

As far as I understood your question, you have created two new fields date and time in items table. Ran the migration successfully and everything works fine in development environment.

BUT the same is not true for test environment. Your tests are failing with an error undefined method 'date' for Item.

That just brings me to the conclusion that you have run the migrations in the development environment but not the test environment.

I would suggest you to use run following command to execute any pending migrations in test environment:

rake db:migrate RAILS_ENV=test

After this run the examples.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top