【发布时间】:2015-07-08 00:34:25
【问题描述】:
我有这门课:
class Zombie < ActiveRecord::Base
attr_accessible :iq
validates :name, presence: true
def genius?
iq >= 3
end
def self.genius
where("iq >= ?", 3)
end
end
我正在做 rspec 测试:
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
我收到此错误消息:
Failures:
1) Zombie with high iq should have a genius count of 1
Failure/Error: Zombie.genius.count.should == 1
expected: 1
got: 0 (using ==)
# zombie_spec.rb:11:in `block (3 levels) '
Finished in 0.2138 seconds
2 examples, 1 failure
Failed examples:
rspec zombie_spec.rb:10 # Zombie with high iq should have a genius count of 1
我使用的语法是:let!(:zombie){...},但它告诉我当我期望 1 时得到了 0。知道吗?也许我花了很多时间查看这段代码,但我看不出问题出在哪里。
【问题讨论】:
-
你真的是在测试僵尸的数量,而不是天才的数量吗?例如
Zombie.count.should == 1
标签: ruby-on-rails ruby testing rspec rspec2