demarches-normaliennes/spec/models/export_spec.rb
Pierre de La Morinerie 4cb747fdb6 specs: always require rails_helper
Test helpers are separated between two files: spec_helper and
rails_helper. This separation is meant to allow tests that do not
require Rails (like testing standalone libs) to boot faster.

The spec_helper file is always loaded, through `--require spec_helper`
in the `.rspec` config file. When needed, the rails_helper file is
expected to be required manually.

This is fine, but:
- Many test files have a redundant `require 'spec_helper'` line;
- Many test files should require `rails_helper`, but don't.

Not requiring `rails_helper` will cause the Rails-concerned section of
the test environment not to be configured–which may cause subtle bugs
(like the test database not being properly initialized).

Moreover, Spring loads all the Rails files on preloading anyway. So the
gains from using only `spec_helper` are thin.

To streamline this process, this commit:
- Configures `.rspec` to require `rails_helper` by default;
- Remove all manual requires to spec_helper or rails_helper.

Reference: https://stackoverflow.com/questions/24145329/how-is-spec-rails-helper-rb-different-from-spec-spec-helper-rb-do-i-need-it
2020-03-31 12:48:32 +02:00

56 lines
1.9 KiB
Ruby

RSpec.describe Export, type: :model do
describe 'validations' do
let(:groupe_instructeur) { create(:groupe_instructeur) }
context 'when everything is ok' do
let(:export) { build(:export) }
it { expect(export.save).to be true }
end
context 'when groupe instructeurs are missing' do
let(:export) { build(:export, groupe_instructeurs: []) }
it { expect(export.save).to be false }
end
context 'when format is missing' do
let(:export) { build(:export, format: nil) }
it { expect(export.save).to be false }
end
end
describe '.stale' do
let!(:export) { create(:export) }
let(:stale_date) { Time.zone.now() - (Export::MAX_DUREE_CONSERVATION_EXPORT + 1.minute) }
let!(:stale_export) { create(:export, updated_at: stale_date) }
it { expect(Export.stale).to match_array([stale_export]) }
end
describe '.destroy' do
let!(:groupe_instructeur) { create(:groupe_instructeur) }
let!(:export) { create(:export, groupe_instructeurs: [groupe_instructeur]) }
before { export.destroy! }
it { expect(Export.count).to eq(0) }
it { expect(groupe_instructeur.reload).to be_present }
end
describe '.find_by groupe_instructeurs' do
let!(:procedure) { create(:procedure) }
let!(:gi_1) { create(:groupe_instructeur, procedure: procedure) }
let!(:gi_2) { create(:groupe_instructeur, procedure: procedure) }
let!(:gi_3) { create(:groupe_instructeur, procedure: procedure) }
context 'when an export is made for one groupe instructeur' do
let!(:export) { Export.create(format: :csv, groupe_instructeurs: [gi_1, gi_2]) }
it { expect(Export.find_for_format_and_groupe_instructeurs(:csv, [gi_1])).to eq(nil) }
it { expect(Export.find_for_format_and_groupe_instructeurs(:csv, [gi_2, gi_1])).to eq(export) }
it { expect(Export.find_for_format_and_groupe_instructeurs(:csv, [gi_1, gi_2, gi_3])).to eq(nil) }
end
end
end