我一直在對抗這種測試方式太久,我不確定我卡在哪裏。這是我想測試模型:rspec codeschool 3級挑戰測試5
class Zombie < ActiveRecord::Base
attr_accessible :iq
validates :name, presence: true
def genius?
iq >= 3
end
def self.genius
where("iq >= ?", 3)
end
end
以下是我與工作啓動:
describe Zombie do
context "with high iq" do
let(:zombie) { Zombie.new(iq: 3, name: 'Anna') }
subject { zombie }
it "should be returned with genius" do
Zombie.genius.should include(zombie)
end
it "should have a genius count of 1" do
Zombie.genius.count.should == 1
end
end
end
這是重構的是工作的一部分:
it { should be_genius }
#it "should have a genius count of 1" do
# Zombie.genius.count.should == 1
#end
這裏是我目前堅持與重構:
describe Zombie do
context "with high iq" do
let!(:zombie) { Zombie.new(iq: 3, name: 'Anna') }
subject { zombie }
it {should include("zombie")}
it { should be_genius }
end
end
根據這個例子,它應該可以工作,但無論我嘗試什麼,它都會對包含進行轟炸。我知道我在這裏失去了一些蹩腳的東西。思考或提示任何人?
當前錯誤消息:
Failures:
1) Zombie with high iq
Failure/Error: it {should include("zombie")}
NoMethodError:
undefined method `include?' for #<Zombie:0x00000006792380>
# zombie_spec.rb:7:in `block (3 levels) '
Finished in 0.12228 seconds
2 examples, 1 failure
Failed examples:
rspec zombie_spec.rb:7 # Zombie with high iq
問問自己:「它」在你的兩個例子中代表什麼? – akatakritos