Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.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 Rspec错误:未读取数组中的扩展方法_Ruby_Rspec - Fatal编程技术网

Ruby Rspec错误:未读取数组中的扩展方法

Ruby Rspec错误:未读取数组中的扩展方法,ruby,rspec,Ruby,Rspec,我不断得到未定义的方法all\u empty?错误。我上课开错了吗 core_extensions.rb class Array def all_empty? self.all? { |element| element.to_s.empty? } end end core_extensions_spec.rb: require "spec_helper" describe Array do context "#all_empty?" do

我不断得到
未定义的方法all\u empty?
错误。我上课开错了吗

core_extensions.rb

class Array
    def all_empty?
        self.all? { |element| element.to_s.empty? }
    end
end
core_extensions_spec.rb:

require "spec_helper"

describe Array do 
    context "#all_empty?" do
        it "returns true if all elements of the Array are empty" do
            expect(["","",""].all_empty?).to be true
        end

        it "returns false if some of the Array elements are not empty" do
            expect(["","1", Object.new, :a].all_empty?).to be false
        end

        it "returns true for an empty Array" do
            expect([].all_empty?).to be true
        end
    end
end

只需在core_extensions_spec.rb中添加一个
require_relative'路径/to/core_extensions.rb'


如果您在其他测试中需要
core\u extensions.rb
,可以将此行添加到
spec\u helper.rb

core\u extension.rb
活动的地方吗?您是否将其包括在测试环境中?您的扩展未加载(在测试环境中),这是什么意思?我如何知道我将其包括在测试环境中?你们是说需要'path/core_extensions.rb'吗?这是有效的,但我很困惑,你们不需要这个文件,这不是意味着有效吗?因为你延长了课程,还是我只是个哑巴?你不是哑巴。为了扩展数组类,需要执行代码。您的计算机上可能有数百个rb脚本,Ruby怎么知道应该解释哪一个呢?你要么需要手动要求所有需要的文件,要么让Rails autoload或Rspec加载所有*_spec.rb文件的魔法发生。我明白了,我不知道为什么我认为它不需要要求,谢谢