demarches-normaliennes/spec/models/champ_shared_example.rb
Pierre de La Morinerie 6328011f60 models: require belong_to associations on champ
- Make `champ.dossier` a requirement;
- Move the dossier_id assignation to `before_validation` (otherwise
the record is invalid, and never gets saved);
- Allow specs to only build the champ (instead of saving it to the
database), which bypasses the requirement to have a dossier.
2020-08-18 15:57:37 +02:00

59 lines
1.6 KiB
Ruby

shared_examples 'champ_spec' do
describe 'mandatory_and_blank?' do
let(:type_de_champ) { build(:type_de_champ, mandatory: mandatory) }
let(:champ) { build(:champ, type_de_champ: type_de_champ, value: value) }
let(:value) { '' }
let(:mandatory) { true }
context 'when mandatory and blank' do
it { expect(champ.mandatory_and_blank?).to be(true) }
end
context 'when carte mandatory and blank' do
let(:type_de_champ) { build(:type_de_champ_carte, mandatory: mandatory) }
let(:value) { '[]' }
it { expect(champ.mandatory_and_blank?).to be(true) }
end
context 'when not blank' do
let(:value) { 'yop' }
it { expect(champ.mandatory_and_blank?).to be(false) }
end
context 'when not mandatory' do
let(:mandatory) { false }
it { expect(champ.mandatory_and_blank?).to be(false) }
end
context 'when not mandatory or blank' do
let(:value) { 'u' }
let(:mandatory) { false }
it { expect(champ.mandatory_and_blank?).to be(false) }
end
end
context "when type_champ=date" do
let(:champ) { build(:champ_date) }
it "should convert %d/%m/%Y format to ISO" do
champ.value = "31/12/2017"
champ.save
champ.reload
expect(champ.value).to eq("2017-12-31")
end
it "should convert to nil if date parse failed" do
champ.value = "bla"
champ.save
champ.reload
expect(champ.value).to be(nil)
end
it "should convert empty string to nil" do
champ.value = ""
champ.save
champ.reload
expect(champ.value).to be(nil)
end
end
end