2016-11-21 6 views
1

私はUndefined method all_empty?エラーが発生し続ける。私はクラスを間違って開けましたか?Rspecエラー:配列の拡張メソッドを読み込まない

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 
+0

'core_extension.rb'はどこに住んでいますか?それをテスト環境に組み込みますか? –

+1

あなたの内線番号はロードされていません(test env) –

+0

あなたはライブで何を意味していますか?それをテスト環境に組み込んだことをどのように知っていますか?あなたはrequire 'path/core_extensions.rb'を意味しますか? –

答えて

2

ちょうどあなたのcore_extensions_spec.rbでrequire_relative 'path/to/core_extensions.rb'を追加します。

他のテストでcore_extensions.rbが必要な場合は、この行をspec_helper.rbに追加できます。

+0

これはうまくいきましたが、私は混乱しています。ファイルを要求する必要がないことを意味するのではないのですか?あなたがクラスを拡張したので、または私はちょうどばかだ? –

+0

あなたはダムではありません。 Arrayクラスを拡張するには、コードを実行する必要があります。あなたのコンピュータにはおそらく100個のrbスクリプトがありますが、Rubyはどの文字を解釈するべきでしょうか?手動で必要なファイルをすべて必要とするか、Railsのautoloadや* _spec.rbファイルをすべてロードするRspecで魔法を起こさせる必要があります。 –

+0

私はそれを必要とする必要はないが、私はなぜ、私はない、ありがとう –

関連する問題