Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.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
需要ruby中的文件和错误_Ruby_Rspec - Fatal编程技术网

需要ruby中的文件和错误

需要ruby中的文件和错误,ruby,rspec,Ruby,Rspec,我想知道是否有人可以帮助澄清require和require relative之间的区别,并帮助解释这里的问题。我的文件结构是 spec/ lion_spec.rb panda_spec.rb lib/ lion.rb panda.rb animal.rb 为了保持代码干燥,我创建了一个Animal类。我的问题是,由于这样做,我得到了一个rspec错误: uninitialized constant Animal (NameError) 我知道这通常意味着我没有正确地要求某

我想知道是否有人可以帮助澄清require和require relative之间的区别,并帮助解释这里的问题。我的文件结构是

spec/
  lion_spec.rb
  panda_spec.rb
lib/
  lion.rb
  panda.rb
  animal.rb
为了保持代码干燥,我创建了一个
Animal
类。我的问题是,由于这样做,我得到了一个rspec错误:

uninitialized constant Animal (NameError)
我知道这通常意味着我没有正确地要求某些东西或创建类,但我似乎找不到,现在我的要求变得一团糟

lion_规格

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
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 
熊猫_规格

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
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
require 'animal'

class Panda < Animal

    def acceptable_food
        [:bamboo]
    end
end
require 'animal'

class Panda < Animal

    def acceptable_food
        [:bamboo]
    end
end
熊猫_规格 需要“动物” 需要“熊猫”

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
描述熊猫做什么
它“应该像竹子”吗
期望(熊猫。新的。喜欢?(:竹子))达到情商(真实)
结束
它“应该像一根绳子一样喜欢竹子”
期望(Panda.new.likes?(“竹子”)。达到情商(true)
结束
它“不应该像蚱蜢那样”
expect(Panda.new.likes?(“蚱蜢”).to eq(false)
结束
结束
**lion.rb**
需要“动物”
狮子类动物
可接受的食物
[:牛羚,:斑马]
结束
结束
**panda.rb**
需要“动物”
熊猫类<动物类
可接受的食物
[:竹子]
结束
结束
**animal.rb**
类动物
喜欢什么?(食物)
可接受的食物。包括?(食物。至sym)
结束
结束

我认为
animal.rb
的代码片段是错误的。你能澄清一下你认为animal.rb是怎么错的吗?因为它显示了
panda.rb
:)你还可以完整地输出rspec吗?我测试了你的代码,它就像一个咒语,你得到的确切错误信息是什么?它发生在哪个文件的哪一行?我同意@Manuel,我复制并粘贴了您的代码,它运行良好。