1
我有一個非常基本的靜態方法對ActiveRecord模型:RSpec的2 speccing靜態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
定義,好象所需#furthest
的Factory.create
調用到不返回nil
。
我在做什麼錯?
你試過定義最遠--- ---除非Comic.maximum(:comic_id)? – corroded
這一切對我來說都是合乎邏輯的。如果輸出'subject'的值,那麼你期望的是什麼?爲了快速完成檢查,如果將示例更改爲'Comic.furthest.should == 0',它是否有所作爲? – d11wtq
我現在已經嘗試了'0,除非Comic.maximum(:comic_id)'沒有改變行爲。 – cfeduke