Rspec错误:没有在Array中读取我的扩展方法

时间:2016-11-21 09:29:34

标签: ruby rspec

我一直收到Undefined method all_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

1 个答案:

答案 0 :(得分:2)

只需在core_extensions_spec.rb中添加require_relative 'path/to/core_extensions.rb'

如果您在其他测试中需要core_extensions.rb,则可以将此行添加到spec_helper.rb