【问题标题】:rspec2 throws 'undefined method' for model class method (.self)rspec2 为模型类方法(.self)抛出“未定义的方法”
【发布时间】:2012-12-05 21:17:49
【问题描述】:

我有一个模型 (event.rb) 方法,它检索指定期间所有重复日期的列表

  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

并得到这个失败:

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

当我将模型从类方法更改为实例方法(删除“self.”)时,控制台只会打印出“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”、...

有什么想法吗?

【问题讨论】:

    标签: ruby-on-rails-3.1 rspec2 rspec-rails


    【解决方案1】:

    所以,我让它工作了,我犯了两个错误:

    • 首先,我需要调用类方法 (Event.dates_between) 而不是实例方法 (Event.new.dates_between)

    • 第二,我期待

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

    但应该已经没有时间了,这在预期的三天内每隔一分钟一小时迭代一次,搞砸了我的控制台

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-22
      • 2019-01-14
      • 1970-01-01
      • 2022-11-03
      • 1970-01-01
      • 2013-02-27
      • 1970-01-01
      • 2019-02-09
      相关资源
      最近更新 更多