Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Testing CodeSchool Rspec示例给了我一个命名错误:';未定义的方法'+';对于nil:NilClass';_Testing_Rspec_Ruby On Rails 4_Rspec Rails_Nomethoderror - Fatal编程技术网

Testing CodeSchool Rspec示例给了我一个命名错误:';未定义的方法'+';对于nil:NilClass';

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

因此,我正在学习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: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