demarches-normaliennes/spec/services/commentaire_service_spec.rb
Pierre de La Morinerie 75a1046315 active_storage: refactor concerns
Follow-up of #5953.

Refactor the concerns with two goals:

- Getting closer from the way ActiveStorage adds its own hooks.
  Usually ActiveStorage does this using an `Attachment#after_create`
  hook, which then delegates to the blob to enqueue the job.
- Enqueuing each job only once. By hooking on `Attachment#after_create`,
  we guarantee each job will be added only once.

We then let the jobs themselves check if they are relevant or not, and
retry or discard themselves if necessary.

We also need to update the tests a bit, because Rails'
`perform_enqueued_jobs(&block)` test helper doesn't honor the `retry_on`
clause of jobs. Instead it forwards the exception to the caller – which
makes the test fail.

Instead we use the inline version of `perform_enqueued_jobs()`, without
a block, which properly ignores errors catched by retry_on.
2021-03-16 11:49:14 +01:00

37 lines
1.1 KiB
Ruby

describe CommentaireService do
include ActiveJob::TestHelper
describe '.create' do
let(:dossier) { create :dossier, :en_construction }
let(:sender) { dossier.user }
let(:body) { 'Contenu du message.' }
let(:file) { nil }
subject(:commentaire) { CommentaireService.build(sender, dossier, { body: body, piece_jointe: file }) }
it 'creates a new valid commentaire' do
expect(commentaire.email).to eq sender.email
expect(commentaire.dossier).to eq dossier
expect(commentaire.body).to eq 'Contenu du message.'
expect(commentaire.piece_jointe.attached?).to be_falsey
expect(commentaire).to be_valid
end
context 'when the body is empty' do
let(:body) { nil }
it 'creates an invalid comment' do
expect(commentaire.body).to be nil
expect(commentaire.valid?).to be false
end
end
context 'when it has a file' do
let(:file) { fixture_file_upload('spec/fixtures/files/piece_justificative_0.pdf', 'application/pdf') }
it 'attaches the file' do
expect(commentaire.piece_jointe.attached?).to be_truthy
end
end
end
end