I would start by placing fixture file(s) in ./spec/fixtures/*.csv
(possibly in a subfolder, if needed). For example, something like:
actual_billable_hours,actual_non_billable_hours,start_date
12,4,2017-10-20
6,7,2017-10-04
In your spec
, you then need to initialise some Project
(s) and Week
(s); then call the method with this fixture file. Something along the lines of:
Rspec.describe 'ActualCsvProjectUpdater' do
describe '.import' do
let!(:project) { create :project, weeks: [week1, week2] }
let(:week1) { create :week, ... }
let(:week2) { create :week, ... }
let(:csv_file) { File.new(fixture_path + '/csv/project_billable_hours.csv') }
it 'updates week included in file' do
descibed_class.import(csv_file, project)
expect(week1.reload.actual_billable_hours).to eq 12
expect(week1.reload.actual_non_billable_hours).to eq 4
end
end
end
This is, of course, only a guideline. For example, I've assumed you are using factory_girl
, but this is not a requirement.
Hopefully this points you in the right direction - you will also benefit from adding a few more tests to cover edge cases. (What if the date is in the future? Or the distant past? Or the date is invalid? Or the billing hours is negative/blank/not a number? What if the csv file contains wrong headers? What if the file is huge, and is causing performance issues? Add tests for whatever your application needs to consider.)