ruby on rails - Rspec test helper -
i having hard time figuring out how test helper because current_group
not defined in test case , not sure how can stub it.
module admin module employeeshelper def upload_access policy(current_group).can_bulk_create_employees? end def dashboard_params download_employee_url = upload_access ? download_admin_group_employees_import_csv_index_path(current_group) : nil upload_employee_url = upload_access ? admin_group_employees_import_csv_index_path(current_group) : nil make_hash(upload_employee_url, download_employee_url) end private def make_hash(upload_url, download_url) { employees: @employees, addemployeeurl: new_admin_group_employee_path(current_group), terminated_employees: @terminated_employees, new_employees: @new_employees, test_employees: @test_employees, group_id: current_group.id, downloademployeeurl: download_url, uploademployeeurl: upload_url } end end end
here's test looks like, fails because current_group
not defined.
require 'rails_helper' describe admin::employeeshelper let!(:group) { create(:group) } before # stub doesn't work because helper doesn't implement current_group allow(helper).to receive(:current_group).and_return(group) end 'returns correct dashboard props' allow(helper).to receive(:upload_access).and_return(true) props = helper.dashboard_params expect(props).values_at(:downloademployeeurl, :uploademployeeurl).should_not include(nil) end end
if problem current_group
method, include helper in dummy class. this:
let(:klass) { class.new include admin::employeeshelper def current_group nil end end } let(:instance) { klass.new } before allow(instance).to receive(:current_group).and_return('whatever') # test-specific group end 'returns correct dashboard props' allow(instance).to receive(:upload_access).and_return(true) props = instance.dashboard_params expect(props).values_at(:downloademployeeurl, :uploademployeeurl).should_not include(nil) end
although foresee you'll have include url helpers too. , set instance vars. in all, isn't worth trouble.
Comments
Post a Comment