demarches-normaliennes/spec/models/commentaire_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

89 lines
2.8 KiB
Ruby

describe Commentaire do
it { is_expected.to have_db_column(:email) }
it { is_expected.to have_db_column(:body) }
it { is_expected.to have_db_column(:created_at) }
it { is_expected.to have_db_column(:updated_at) }
it { is_expected.to belong_to(:dossier) }
describe 'messagerie_available validation' do
subject { commentaire.valid?(:create) }
context 'with a commentaire created by the DS system' do
let(:commentaire) { build :commentaire, email: CONTACT_EMAIL }
it { is_expected.to be_truthy }
end
context 'on an archived dossier' do
let(:dossier) { create :dossier, :archived }
let(:commentaire) { build :commentaire, dossier: dossier }
it { is_expected.to be_falsey }
end
context 'on a dossier en_construction' do
let(:dossier) { create :dossier, :en_construction }
let(:commentaire) { build :commentaire, dossier: dossier }
it { is_expected.to be_truthy }
end
end
describe "#sent_by_system?" do
subject { commentaire.sent_by_system? }
let(:commentaire) { build :commentaire, email: email }
context 'with a commentaire created by the DS system' do
let(:email) { CONTACT_EMAIL }
it { is_expected.to be_truthy }
end
end
describe "#redacted_email" do
subject { commentaire.redacted_email }
context 'with a commentaire created by a instructeur' do
let(:commentaire) { build :commentaire, instructeur: instructeur }
let(:instructeur) { build :instructeur, email: 'some_user@exemple.fr' }
it { is_expected.to eq 'some_user' }
end
context 'with a commentaire created by a user' do
let(:commentaire) { build :commentaire, user: user }
let(:user) { build :user, email: 'some_user@exemple.fr' }
it { is_expected.to eq 'some_user@exemple.fr' }
end
end
describe "#notify" do
let(:procedure) { create(:procedure) }
let(:instructeur) { create(:instructeur) }
let(:assign_to) { create(:assign_to, instructeur: instructeur, procedure: procedure) }
let(:user) { create(:user) }
let(:dossier) { create(:dossier, :en_construction, procedure: procedure, user: user) }
let(:commentaire) { Commentaire.new(dossier: dossier, body: "Mon commentaire") }
context "with a commentaire created by a instructeur" do
it "calls notify_user" do
expect(commentaire).to receive(:notify_user)
commentaire.email = instructeur.email
commentaire.save
end
end
context "with a commentaire automatically created (notification)" do
it "does not call notify_user or notify_instructeurs" do
expect(commentaire).not_to receive(:notify_user)
expect(commentaire).not_to receive(:notify_instructeurs)
commentaire.email = CONTACT_EMAIL
commentaire.save
end
end
end
end