Josef Tinagan Josef Tinagan - 15 days ago 5
Ruby Question

Rspec Error: Not reading my extended method in Array

I keep getting

Undefined method all_empty?
error. Did I open the class incorrectly?

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

Answer

Just add a require_relative 'path/to/core_extensions.rb' in your core_extensions_spec.rb.

If you need core_extensions.rb in other tests, you can add this line to your spec_helper.rb.

Comments