demarches-normaliennes/spec/helpers/commentaire_helper_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

78 lines
2.5 KiB
Ruby
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

RSpec.describe CommentaireHelper, type: :helper do
let(:commentaire) { create(:commentaire, email: "michel@pref.fr") }
describe ".commentaire_is_from_me_class" do
subject { commentaire_is_from_me_class(commentaire, me) }
context "when commentaire is from me" do
let(:me) { User.new(email: "michel@pref.fr") }
it { is_expected.to eq("from-me") }
end
context "when commentaire is not from me" do
let(:me) { User.new(email: "roger@usager.fr") }
it { is_expected.to eq nil }
end
end
describe '.commentaire_answer_action' do
subject { commentaire_answer_action(commentaire, me) }
context "when commentaire is from me" do
let(:me) { User.new(email: "michel@pref.fr") }
it { is_expected.to include('Envoyer') }
end
context "when commentaire is not from me" do
let(:me) { User.new(email: "roger@usager.fr") }
it { is_expected.to include('Répondre') }
end
end
describe '.commentaire_is_from_guest' do
let(:dossier) { create(:dossier, :en_instruction) }
let!(:guest) { create(:invite, dossier: dossier) }
subject { commentaire_is_from_guest(commentaire) }
context 'when the commentaire sender is not a guest' do
let(:commentaire) { create(:commentaire, dossier: dossier, email: "michel@pref.fr") }
it { is_expected.to be false }
end
context 'when the commentaire sender is a guest on this dossier' do
let(:commentaire) { create(:commentaire, dossier: dossier, email: guest.email) }
it { is_expected.to be true }
end
end
describe '.commentaire_date' do
let(:present_date) { Time.zone.local(2018, 9, 2, 10, 5, 0) }
let(:creation_date) { present_date }
let(:commentaire) do
Timecop.freeze(creation_date) { create(:commentaire, email: "michel@pref.fr") }
end
subject do
Timecop.freeze(present_date) { commentaire_date(commentaire) }
end
it 'doesnt include the creation year' do
expect(subject).to eq 'le 2 septembre à 10 h 05'
end
context 'when displaying a commentaire created on a previous year' do
let(:creation_date) { present_date.prev_year }
it 'includes the creation year' do
expect(subject).to eq 'le 2 septembre 2017 à 10 h 05'
end
end
context 'when formatting the first day of the month' do
let(:present_date) { Time.zone.local(2018, 9, 1, 10, 5, 0) }
it 'includes the ordinal' do
expect(subject).to eq 'le 1er septembre à 10 h 05'
end
end
end
end