2016-09-02 12 views
0

Ruby、Rails、rspecの新機能です。私は、次の失敗を取得rpecの失敗原因を理解していませんか?

describe '#sorted_college_list_for_degrees' do 
before do 
    stub_const("COLLEGE_AND_DEPARTMENT", 
       {"current_colleges_for_degrees"=> 
       { 
        "ceas"=>{"label"=>"Engineering"}, 
        "com"=>{"label"=>"College of Medicine"}, 
        "a&s"=>{"label"=>"Arts & Sciences"} 
       } 
       } 
      ) 
end 

it "should return an array" do 
expect(helper.sorted_college_list_for_degrees).to be_an(Array) 
end 

it "should contain all the colleges for degrees, plus 'other'" do 
    expect(helper.sorted_college_list_for_degrees).to eq(
    ['Arts & Sciences','College of Medicine','Engineering','Other'] 
) 
end 
end 

describe '#sorted_college_list_for_generic_works' do 
before do 
    stub_const("COLLEGE_AND_DEPARTMENT", 
       {"current_colleges_for_degrees"=> 
       { 
        "ceas"=>{"label"=>"Engineering"}, 
        "com"=>{"label"=>"Arts & Sciences"}, 
       }, 
       "additional_current_colleges"=> 
       { 
        "ucl"=>{"label"=>"Libraries"}, 
        "ucba"=>{"label"=>"Blue Ash College"}, 
       }, 
       } 
      ) 
end 

it "should return an array" do 
    expect(helper.sorted_college_list_for_degrees).to be_an(Array) 
end 

it "should contain all the colleges for degrees, plus additional colleges, plus 'other'" do 
    expect(helper.sorted_college_list_for_generic_works).to eq(
    ['Arts & Sciences','Blue Ash College','Engineering','Libraries','Other'] 
) 
end 

エンド

:私は、私は書いていない次のコードを使用してテストを実行しています

ApplicationHelper#** sorted_college_list_for_degreesは度のために、すべての大学が含まれている必要がありますが、プラス 'その他' **エラー/エラー:(helper.sorted_college_list_for_degrees).to eq( タイプエラー: ハッシュへの暗黙的な変換なし

"RSpecの/Users/lisa/workspaces/curate/spec/helpers/curate_helper_spec.rb:257#ApplicationHelper#sorted_college_list_for_degrees度のために、すべての大学が含まれている必要があり、加えて 'その他'"

だから、私は私はエラーを理解しているか分からない。元ヘルパーファイルで、この方法があることに注意してください:事前

+0

質問を編集して失敗した仕様のみを含むようにすると、読者はより簡単になります。 –

+0

それを指摘していただきありがとうございます。 – LisaH

答えて

0

def sorted_college_list_for_degrees 
    list = COLLEGE_AND_DEPARTMENT["current_colleges_for_degrees"].merge(
     COLLEGE_AND_DEPARTMENT["additional_current_colleges"] 
    ) 
    list.keys.collect do |k| 
     list[k]["label"] 
    end.sort << "Other" 
end 

おかげであなたは

COLLEGE_AND_DEPARTMENT["additional_current_colleges"] 

に結果の値を持つ定数がnilビーイングとにnilを渡すことをスタブされていますmergeは許可されていません。ハッシュが必要です。

アプリケーションコードは、この値がnilであるかどうかをチェックし、そうでない場合はコールマージのみを行うか、アプリケーションコードが予期しているものと一致するようにスタブデータを変更する必要があります。

+0

ああ、大丈夫です。私は今それを得ると思う! – LisaH

関連する問題