【发布时间】:2014-04-04 02:54:05
【问题描述】:
我已经与这个测试作斗争太久了,我不确定我在哪里卡住了。这是我要测试的模型:
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
【问题讨论】:
-
问问自己:在你的两个例子中,“它”代表什么?
标签: ruby ruby-on-rails-3 rspec tdd