LisaH LisaH - 3 months ago 7
Ruby Question

Dont understand rpec failure cause?

New to Ruby, Rails and rspec. I am running a test with the following code I did not write:

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


end

I get the following failure:

ApplicationHelper#**sorted_college_list_for_degrees should contain all the colleges for degrees, plus 'other'
**Failure/Error:
expect(helper.sorted_college_list_for_degrees).to eq(
TypeError:
no implicit conversion of nil into Hash

"rspec /Users/lisa/workspaces/curate/spec/helpers/curate_helper_spec.rb:257 # ApplicationHelper#sorted_college_list_for_degrees should contain all the colleges for degrees, plus 'other'"

So-I am not sure I understand the error. Note that in the original helper file there is this method:

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


thanks in advance

Answer

You are stubbing that constant with a value that results in

COLLEGE_AND_DEPARTMENT["additional_current_colleges"]

Being nil and passing nil to merge is not allowed - it expects a hash.

Either the application code should check whether this value is nil and only call merge if it not or you should change the stubbed data to match what the application code is expecting.