我想知道是否有人可以帮助澄清要求和要求亲属之间的差异,并帮助解释这里的问题。我的文件结构是
spec/
lion_spec.rb
panda_spec.rb
lib/
lion.rb
panda.rb
animal.rb
为了保持代码干燥,我创建了一个Animal
类。我的问题是,因为这样做,我得到一个rspec错误:
uninitialized constant Animal (NameError)
我知道这通常意味着我没有正确地要求或创建课程,但我似乎无法找到这一点,现在我对这些要求感到困惑!
lion_spec
require 'animal'
require 'lion'
describe Lion do
it "should like wildebeests" do
expect(Lion.new.likes?(:wildebeests)).to eq(true)
end
it "should like zebras" do
expect(Lion.new.likes?(:zebras)).to eq(true)
end
it "should not like salad" do
expect(Lion.new.likes?(:salad)).to eq(false)
end
end
panda_spec
require 'animal'
require 'panda'
describe Panda do
it 'should like bamboo' do
expect(Panda.new.likes?(:bamboo)).to eq(true)
end
it 'should like bamboo as a string' do
expect(Panda.new.likes?("bamboo")).to eq(true)
end
it 'should not like grasshoppers' do
expect(Panda.new.likes?("grasshoppers")).to eq(false)
end
end
lion.rb
require 'animal'
class Lion < Animal
def acceptable_food
[:wildebeests, :zebras]
end
end
panda.rb
require 'animal'
class Panda < Animal
def acceptable_food
[:bamboo]
end
end
animal.rb
require 'animal'
class Panda < Animal
def acceptable_food
[:bamboo]
end
end
测试覆盖率很好我修改为以下内容:
**lion_spec**
require 'animal'
require 'lion'
describe Lion do
it "should like wildebeests" do
expect(Lion.new.likes?(:wildebeests)).to eq(true)
end
it "should like zebras" do
expect(Lion.new.likes?(:zebras)).to eq(true)
end
it "should not like salad" do
expect(Lion.new.likes?(:salad)).to eq(false)
end
end
panda_spec 要求动物&#39; 要求&#p; panda&#39;
describe Panda do
it 'should like bamboo' do
expect(Panda.new.likes?(:bamboo)).to eq(true)
end
it 'should like bamboo as a string' do
expect(Panda.new.likes?("bamboo")).to eq(true)
end
it 'should not like grasshoppers' do
expect(Panda.new.likes?("grasshoppers")).to eq(false)
end
end
**lion.rb**
require 'animal'
class Lion < Animal
def acceptable_food
[:wildebeests, :zebras]
end
end
**panda.rb**
require 'animal'
class Panda < Animal
def acceptable_food
[:bamboo]
end
end
**animal.rb**
class Animal
def likes?(food)
acceptable_food.include?(food.to_sym)
end
end