Reputation: 2900
I have simple CSV rails generator which tbh I've no idea how to test it with rspec. I was trying to follow this article https://making.dia.com/testing-csv-files-in-rails-on-the-fly-7a1285cc2aac and that one too Testing CSV.generate with RSpec but I just simply don't understand what is going on there, these examples seem to be different.
module AdminLogData
class CsvAdminLogGenerator
LOG_HEADERS = ['Date&Time', 'Action', 'New Data'].freeze
def initialize(start_date:, end_date:)
@start_date = start_date
@end_date = end_date
end
def call
AdminPanelLog.dates_between(start_date, end_date).find_each do |admin_log|
CSV.generate(headers: LOG_HEADERS, col_sep: ';', encoding: 'UTF-8') do |csv|
csv << admin_log.created_at
csv << admin_log.action_type
csv << admin_log.admin_email
csv << admin_log.new_data
end
end
end
end
end
Could someone explain me how to test this class? should I create some fake csv first?
spec
RSpec.describe AdminLogData::CsvAdminLogGenerator do
subject(:csv_file) { described_class.new(start_date, end_date).call }
let(:start_date) { 3.months.ago }
let(:end_date) { 2.months.ago }
let(:header) { 'column1, column2, column3' }
let(:row2) { 'value1, value2, value3' }
let(:row3) { 'value1, value2, value3' }
let(:rows) { [header, row2, row3] }
it 'creates CSV file with proper value' do
expect(csv).to receive(:<<).with(log.created_at)
end
end
Edit:
expected collection contained: ["2019-06-17 22:13:48 +0200,New,[email protected],\"{\"\"email\"\"=>\"\"[email protected]\"\", \"\"ro...=>\"\"2019-09-16T22:13:48.752+02:00\"\", \"\"other_activities\"\"=>\"\"forbidden websites\"\"}\"\n"]
actual collection contained: ["2019-06-17 22:13:48 +0200", "New", "[email protected]", "{\"email\"=>\"[email protected]\... \"last_update\"=>\"2019-09-16T22:13:48.752+02:00\", \"other_activities\"=>\"forbidden websites\"}"]
the missing elements were: ["2019-06-17 22:13:48 +0200,New,[email protected],\"{\"\"email\"\"=>\"\"[email protected]\"\", \"\"ro...=>\"\"2019-09-16T22:13:48.752+02:00\"\", \"\"other_activities\"\"=>\"\"forbidden websites\"\"}\"\n"]
the extra elements were: ["2019-06-17 22:13:48 +0200", "New", "[email protected]", "{\"email\"=>\"[email protected]\... \"last_update\"=>\"2019-09-16T22:13:48.752+02:00\", \"other_activities\"=>\"forbidden websites\"}"]
Upvotes: 1
Views: 4076
Reputation: 1238
Step 1: Create a factory for AdminLogData
Using the gem factory_bot combined with faker, you will be able to generate fake AdminLogData objects for your tests.
Step 2: Write your test
In your test, you'll more likely want to test that the CSV lines have the right data instead that the class receives the "<<" message. For instance it could look like that:
RSpec.describe AdminLogData::CsvAdminLogGenerator do
subject(:csv_file) { described_class.new(start_date, end_date).call }
let(:start_date) { 3.months.ago }
let(:end_date) { 2.months.ago }
let(:admin_log_data) { FactoryBot.create(:admin_log_data, created_at: 3.months.ago) }
before { admin_log_data }
it 'creates CSV file with proper value' do
expect(csv_file.to_a[1]).to match_array(CSV.generate_line([
admin_log_data.created_at
admin_log_data.action_type
admin_log_data.admin_email
admin_log_data.new_data
]))
end
end
Upvotes: 2