Below is the unit test I wrote for a small piece of Ruby code.
subject { AutoShop.new 'Mr. Fix It', 'WA987654321', Employee.new('Sue', 'Owner', 0) }
it '#employee_list returns list of employee names' do
# setup
input = [Employee.new('Lou', 'Receptionist', 90_000)]
subject.append_employees input
subject.employee_list.should eq "Sue\nLou"
end
And here is the code for the class. However, I cannot get the names to display properly in the employee_list method. I’m brand new to Ruby, so any suggestions would be greatly appreciated.
class AutoShop < Business
attr_accessor :employees
def initialize(name, tax_id, employee)
super(name, tax_id)
@employees = []
@employees << employee
end
def append_employees(input)
input.map { |x| @employees << x }
end
def employee_list
@employees.map {|x| x.name}
end
end
Your
employee_listmethod, based on your unit test, should output each name separated by a linebreak\n. So, add a.join("\n")to produce a string from the array.The
.mapcan be done with a “pretzel-colon” shorthand too: