2016-04-04 4 views
-3

:残念ながら、次の試験はRubyの配列のオープンクラスにメソッドを追加する私は、コアのRuby <code>Array</code>クラスに<code>all_empty?</code>メソッドを追加しようとしています

class Array 
    def all_empty? 
    ... 
    end 
end 

は失敗:

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_truthy 
    end 
    end 
end 

Iを次のNoMethodErrorを取得します。

NoMethodError: 
    undefined method `all_empty?' for ["", "", ""]:Array 
    Did you mean? empty? 
# ./spec/core_extensions_spec.rb:6:in `block (3 levels) in <top (required)>' 

仕様をr既にall_empty?メソッドを定義していることを認識していますか?

EDIT:これは、以下のコメントにするために頼まれspec_helper.rbファイルの内容です:

$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__) 
require_relative "../lib/tic_tac_toe.rb" 
+1

「spec_helper」とは何ですか?コードはどのように読み込まれますか? – sawa

+2

'Array' Monkeypatchがロードされていることを確認してください._before_ testが実行されています。 – mudasobwa

答えて

0

あなたは、あなたが定義する方法(all_empty?)をqccessためにあなたのスペックにファイルをrequire必要がありますこれを配列化する。またはspec_helper.rbで指定することができます

rspecはデフォルトでロードローカルパスlibをロードパスに追加します。

letが、それはあなたべきrequireこの

require `array_method` 

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

またはspec_helper.rb

require `array_method` 

、その後

におけるスペックで今 lib/array_method.rb

class Array 
    def all_empty? 
    ... 
    end 
end 

であると言います

あなたのコード

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_truthy 
    end 
    end 
end 
関連する問題