Ruby,Rails和rspec的新手。我正在使用以下代码运行测试:
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( 类型错误: 没有将nil隐式转换为Hash
“rspec /Users/lisa/workspaces/curate/spec/helpers/curate_helper_spec.rb:257#ApplicationHelper#sorted_college_list_for_degrees应该包含所有学位,加上'其他'”
所以 - 我不确定我是否理解错误。请注意,在原始帮助文件中有以下方法:
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
提前致谢
答案 0 :(得分:0)
您使用导致
的值来存储该常量COLLEGE_AND_DEPARTMENT["additional_current_colleges"]
不允许nil并且将nil传递给merge
- 它需要哈希值。
应用程序代码应检查此值是否为nil,如果不是,则仅调用merge,或者应更改存根数据以匹配应用程序代码所期望的值。