Testing CodeSchool Rspec示例给了我一个命名错误:';未定义的方法'+';对于nil:NilClass';
因此,我正在学习CodeSchool的Rspec课程(我在4级),我喜欢重写示例,以加强我所学的内容。我设置了一个模仿僵尸类的Dog类,并运行了相同的测试,但由于某种原因,我得到了错误:Testing CodeSchool Rspec示例给了我一个命名错误:';未定义的方法'+';对于nil:NilClass';,testing,rspec,ruby-on-rails-4,rspec-rails,nomethoderror,Testing,Rspec,Ruby On Rails 4,Rspec Rails,Nomethoderror,因此,我正在学习CodeSchool的Rspec课程(我在4级),我喜欢重写示例,以加强我所学的内容。我设置了一个模仿僵尸类的Dog类,并运行了相同的测试,但由于某种原因,我得到了错误: 1) Dog is a genius dog Failure/Error: before { dog.learn_trick } NoMethodError: undefined method `+' for nil:NilClass # ./app/models/dog.rb:19
1) Dog is a genius dog
Failure/Error: before { dog.learn_trick }
NoMethodError:
undefined method `+' for nil:NilClass
# ./app/models/dog.rb:19:in `learn_trick'
# ./spec/models/dog_spec.rb:23:in `block (2 levels) in <top (required)>'
2) Dog is not a dummy dog
Failure/Error: before { dog.learn_trick }
NoMethodError:
undefined method `+' for nil:NilClass
# ./app/models/dog.rb:19:in `learn_trick'
# ./spec/models/dog_spec.rb:23:in `block (2 levels) in <top (required)>'
我的规格
describe Zombie do
let(:zombie) { Zombie.new }
subject { zombie }
before { zombie.eat_brains }
it 'is not a dummy zombie' do
zombie.should_not be_dummy
end
it 'is a genius zombie' do
zombie.should be_genius
end
end
describe Dog do
let(:dog) { Dog.new }
subject { dog }
before { dog.learn_trick }
it "is not a dummy dog" do
dog.should_not be_dummy
end
it "is a genius dog" do
dog.should be_genius
end
end
有人能解释一下为什么我会得到这个名字吗?
另外,我知道这个网站上的问题通常都是向实用的方向学习的,但希望了解我为什么会出现这个错误将有助于我以后编写更多的实用测试。
谢谢 智商必须有一个值才能增加。所以你需要做的是在self.iq+=3之前
,你还没有在新记录中初始化iq
,所以它的值是nil
,你试图以这种方式递增它是失败的。
describe Zombie do
let(:zombie) { Zombie.new }
subject { zombie }
before { zombie.eat_brains }
it 'is not a dummy zombie' do
zombie.should_not be_dummy
end
it 'is a genius zombie' do
zombie.should be_genius
end
end
describe Dog do
let(:dog) { Dog.new }
subject { dog }
before { dog.learn_trick }
it "is not a dummy dog" do
dog.should_not be_dummy
end
it "is a genius dog" do
dog.should be_genius
end
end