I've got a pretty basic static method on an ActiveRecord model:
#./app/models/comic.rb
class Comic < ActiveRecord::Base
class << self
def furthest
Comic.maximum(:comic_id) || 0
end
end
end
When executing Comic.furthest
in the Rails console it returns 0 as I expect. The problem is I am trying to spec this behavior for both the presence and absence of records:
#./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
All of this appears very basic and straightforward, however my specs are failing with the message:
1) Comic#furthest when there are no rows in the database
Failu开发者_运维问答re/Error: it { should == 0 }
expected: 0
got: nil (using ==)
# ./spec/models/comic_spec.rb:8:in `block (4 levels) in <top (required)>'
Even if I change furthest
to simply:
def furthest
0
end
I still get nil (using ==)
.
The second spec, it { should == 100 }
passes with the original Comic.maximum(:comic_id) || 0
definition, as if the Factory.create
invocations are required for #furthest
to not return nil
.
What am I doing wrong?
I am fairly confident this was a problem with me using the p180 release of Ruby 1.9.2 with custom patches to improve require performance. After upgrading to p290 this problem is gone.
精彩评论