spec/models/study_event_spec.rb
# frozen_string_literal: true
RSpec.describe StudyEvent, type: :model do
subject { TestFactory.test_study_event }
let!(:arm) { TestFactory.test_arm }
let!(:schedule) { TestFactory.test_schedule }
let!(:study) { TestFactory.test_study }
include_examples 'valid object creation', StudyEvent
describe '#respond_to?' do
include_context 'shared attributes'
include_examples 'respond', %i[arm schedule study name]
include_examples 'respond', %i[order study_event_instruments instruments]
include_examples 'common attributes'
end
include_examples 'required attribute', 'name', 2
include_examples 'required attribute', 'order', 2
include_examples 'default attribute', 'order', 1
describe '#order' do
include_context 'restore attributes'
include_examples 'number specs', 'order'
end
include_examples 'invalid create', 'without name or arm'
describe '.create!' do
context '1 character name' do
it { expect { described_class.create! name: 'a', arm: arm }.to raise_error ActiveRecord::RecordInvalid }
end
context '2+ character name and title' do
it { expect { described_class.find_or_create_by! name: 'as', arm: arm }.to_not raise_error }
end
context 'has unique name' do
it do
rs = described_class.create! name: 'a12', arm: arm
begin
expect { described_class.create! name: 'a12', arm: arm }.to raise_error ActiveRecord::RecordInvalid
ensure
rs.destroy!
end
end
end
end
describe '#to_s' do
it do
eager_subject = StudyEvent.includes(arm: { schedule: :study })
.where(name: TestConstants::TEST_STUDY_EVENT, arm: arm)
.first
expect(eager_subject.to_s).to eq "#{arm} #{TestConstants::TEST_STUDY_EVENT}"
end
end
end