rspec2 throws 'undefined method' for model class method (.self)-Collection of common programming errors

I have a model (event.rb) method, that retrieves a list of all recurrence dates for the named period

  def self.dates_between(start_date, end_date)
     dates = (start_date..end_date).step(7).to_a
  end

than I specify the following in event_spec.rb

  before(:each) do
    @event = FactoryGirl.create(:event)
  end    
  subject { @event }

  ... other working tests ...

  describe '#dates_between' do
    context 'finds recurrences dates of a event' do
      start_date = "2012-12-01 18:25:25"
      end_date = "2012-12-15 18:25:25"
      output_dates = ["2012-12-01 18:25:25", "2012-12-08 18:25:25", "2012-12-15 18:25:25"]

      it 'should call Event with method dates_between' do
        @event.should_receive(:dates_between).with(start_date, end_date)
        @event.dates_between(start_date, end_date)
      end

      it 'should find and return the RIGHT recurrences dates' do
        @event.dates_between(start_date, end_date).should eq(output_dates)
      end
    end
  end

and get this failure:

1) Event#dates_between finds recurrences dates of a event should find and return the RIGHT recurrences dates
 Failure/Error: @event.dates_between(start_date, end_date).should eq(output_dates)
 NoMethodError:
   undefined method `dates_between' for #
 # ./spec/models/event_spec.rb:52:in `block (4 levels) in '

when I change in the model from a class method to an instance method (removing “self.”) the console just prints out “wild data”:

22:93:55″, “2012-12-01 22:93:62”, “2012-12-01 22:93:69”, “2012-12-01 22:93:76”, “2012-12-01 22:93:83”, “2012-12-01 22:93:90”, “2012-12-01 22:93:97”, “2012-12-01 22:94:04”, “2012-12-01 22:94:11”, “2012-12-01 22:94:18”, “2012-12-01 22:94:25”, “2012-12-01 22:94:32”, …

any ideas?

  1. So, I got it working, I made two faults:

    • First, I needed to call on a class method (Event.dates_between) and not on an instance method (Event.new.dates_between)

    • Second, I expected

      [“2012-12-01 18:25:25”, “2012-12-08 18:25:25”, “2012-12-15 18:25:25”]

    but should have expexted without time, which screwed up my console by iterating through every second – minute – hour for the three expected days

    ["2012-12-01", "2012-12-08", "2012-12-15"] 
    

    No I do following and the specs are green:

    describe Event do
    
      subject(:event) { FactoryGirl.create(:event) }
    
      describe '#dates_between' do
        context 'finds recurrences dates of a event' do
          start_date = "2012-12-01"
          end_date = "2012-12-15"
          output_dates = ["2012-12-01", "2012-12-08", "2012-12-15"]
    
          it 'should call dates_between with two arguments' do
            event.should_receive(:dates_between).with(start_date, end_date).and_return(output_dates)
            event.dates_between(start_date, end_date).should eq(output_dates)
          end
    
          it 'should find and return the RIGHT recurrences dates' do
            Event.dates_between(start_date, end_date).should eq(output_dates)
          end
        end
      end
    
    end
    

Originally posted 2013-11-09 21:39:49.