RSpec 2指定静态ActiveRecord方法

时间:2011-07-02 05:22:27

标签: ruby rspec2

我在ActiveRecord模型上有一个非常基本的静态方法:

#./app/models/comic.rb
class Comic < ActiveRecord::Base
  class << self
    def furthest
      Comic.maximum(:comic_id) || 0
    end
  end
end

在Rails控制台中执行Comic.furthest时,它会像我预期的那样返回0。问题是我试图为记录的存在和缺失规定这种行为:

#./spec/app/models/comic_spec.rb
require 'spec_helper'

describe Comic do
  describe "#furthest" do
    subject { Comic.furthest }

    context "when there are no rows in the database" do
      it { should == 0 }
    end

    context "when there are rows in the database" do
      before do
        Factory.create(:comic, :comic_id => 100)
        Factory.create(:comic, :comic_id => 99)
      end

      it { should == 100 }
    end
  end
end

所有这些看起来都非常基本和直接,但是我的规格却没有显示消息:

1) Comic#furthest when there are no rows in the database 
 Failure/Error: it { should == 0 }
   expected: 0
        got: nil (using ==)
 # ./spec/models/comic_spec.rb:8:in `block (4 levels) in <top (required)>'

即使我将furthest更改为:

def furthest
  0
end

我仍然得到nil (using ==)

第二个规范it { should == 100 }与原始Comic.maximum(:comic_id) || 0定义一起传递,好像Factory.create调用不需要返回#furthest。< / p>

我做错了什么?

1 个答案:

答案 0 :(得分:0)

我相当自信这是一个问题,我使用p180发布的Ruby 1.9.2和自定义补丁来提高性能要求。升级到p290后,这个问题就消失了。