25
loading...
This website collects cookies to deliver better user experience
spec/fixtures/files
(or in your own customized path). This way, you can read an actual file in your tests or just pass the file path to other classes that might need to access the file data. # students_uploader_spec.rb
describe 'Services::StudentsUploader' do
it 'creates students' do
Tempfile.open(['students.csv', '.csv']) do |temp|
CSV.open(temp, 'wb', col_sep: ';', headers: true) do |csv|
csv << ['email', 'name']
csv << ['[email protected]', 'Student Name']
end
result = described_class.call(temp)
expect(result[:count]).to eq(1)
expect(Student.count).to eq(1)
end
end
end
Failures:
Failure/Error: result = described_class.call(temp)
NameError:
undefined local variable or method `temp' for #<RSpec::ExampleGroups::Services::SudentsUploader:0x00007fb74000c0b8>
# ./spec/services/students_uploader_spec.rb:1:in `block (3 levels) in <top (required)>'
describe 'Services::StudentsUploader' do
context 'when uploading a xlsx' do
it 'creates students' do
Tempfile.open(['students.xlsx', '.xlsx']) do |temp|
Axlsx::Package.new do |p|
p.workbook.add_worksheet(name: "test") do |sheet|
sheet.add_row ['name', 'email', 'code']
sheet.add_row ['Jane Doe', nil, 123]
sheet.add_row ['John Doe', '[email protected]', nil]
end
p.serialize(temp.path)
end
result = described_class.call(temp.path)
expect(result[:count]).to eq(1)
expect(Student.count).to eq(1)
end
end
end
end