2012-12-05 26 views
0

我有一個模型(event.rb)方法,檢索所有的復發日期的列表爲命名期間rspec2拋出「未定義的方法」的模型類的方法(.self)

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

比我指定在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 

以下,並得到此故障:當我從CLAS更改模型

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 #<Event:0xb99e9f8> 
# ./spec/models/event_spec.rb:52:in `block (4 levels) in <top (required)>' 

(除去「自我」)控制檯只打印出「野生數據」:

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「,...

有什麼想法?

回答

0

於是,我懂了工作,我做了兩個錯誤:

  • 首先,我需要在一個類的方法(Event.dates_between),而不是一個實例方法調用(Event.new.dates_between )

  • 其次,我希望

    [ 「2012年12月1日18時25分25秒」, 「2012年12月8日18時25分25秒」,「二○一二年十二月十五日18:25 :25「]

但應該沒有時間已經expexted,它通過每秒迭代搞砸了我的控制檯 - 分鐘 - 小時的三項預期天

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

不,我不跟隨和規格是綠色:

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 
相關問題