Skip to content

Commit

Permalink
add rspec shared destroy context
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenbeales committed Mar 27, 2018
1 parent 6ba1fd0 commit 3954d8c
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 36 deletions.
18 changes: 8 additions & 10 deletions spec/models/assessment_instrument_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,49 +66,47 @@
context 'no instrument' do
it do
expect do
described_class.create! assessment: assess
AssessmentInstrument.create! assessment: assess
end.to raise_error ActiveRecord::RecordInvalid
end
end

context 'no assessment' do
it do
expect do
described_class.create! instrument: instrument
AssessmentInstrument.create! instrument: instrument
end.to raise_error ActiveRecord::RecordInvalid
end
end

context 'with assessment and instrument' do
it do
expect do
described_class.find_or_create_by instrument: instrument,
assessment: assess
AssessmentInstrument.find_or_create_by instrument: instrument,
assessment: assess
end.not_to raise_error
end
end

context 'unique instrument and assessment' do
it do
expect do
described_class.create!(instrument: instrument, assessment: assess)
AssessmentInstrument.create!(instrument: instrument, assessment: assess)
end.to raise_error ActiveRecord::RecordInvalid
end
end
end

describe '#to_s' do
it do
expect(described_class.where(assessment: assess.id,
instrument: instrument.id) \
expect(AssessmentInstrument.where(assessment: assess.id,
instrument: instrument.id) \
.first.to_s).to eq("#{assess} #{instrument}")
end
end

describe '#destroy!' do
before :each do
subject.destroy!
end
include_context 'destroy subject'

it { expect(Instrument.exists?(instrument.id)).to be_truthy }

Expand Down
12 changes: 6 additions & 6 deletions spec/models/folder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,30 +42,30 @@
describe '.create!' do
context 'without a name' do
it do
expect { described_class.create! }.to raise_error \
expect { Folder.create! }.to raise_error \
ActiveRecord::RecordInvalid
end
end

context 'with 1 character name' do
it do
expect { described_class.create! name: 'a' }.to raise_error \
expect { Folder.create! name: 'a' }.to raise_error \
ActiveRecord::RecordInvalid
end
end

context 'with 2+ character name' do
it do
expect do
described_class.find_or_create_by! name: 'as'
Folder.find_or_create_by! name: 'as'
end.to_not raise_error
end
end

context 'with unique name' do
it do
rs = described_class.create! name: 'a12'
expect { described_class.create! name: 'a12' }.to \
rs = Folder.create! name: 'a12'
expect { Folder.create! name: 'a12' }.to \
raise_error ActiveRecord::RecordInvalid
rs.destroy!
end
Expand All @@ -75,7 +75,7 @@
describe '#destroy!' do
context 'with projects' do
it do
folder = described_class.create! name: 'a13'
folder = Folder.create! name: 'a13'
folder.projects.concat [project1, project2]
folder.destroy!
expect(project1).not_to be_nil
Expand Down
8 changes: 3 additions & 5 deletions spec/models/study_event_instrument_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

describe '#respond_to?' do
include_context 'shared attributes'

it { expect(subject.respond_to?(:study_event)).to be_truthy }
it { expect(subject.respond_to?(:instrument)).to be_truthy }
context 'common attributes' do
Expand Down Expand Up @@ -81,10 +81,8 @@
end

describe '#destroy!' do
before :each do
subject.destroy!
end

include_context 'destroy subject'

it { expect(Instrument.exists?(instrument.id)).to be_truthy }

it { expect(StudyEvent.exists?(study_event.id)).to be_truthy }
Expand Down
15 changes: 7 additions & 8 deletions spec/models/study_participant_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

describe '#respond_to?' do
include_context 'shared attributes'

it { expect(subject.respond_to?(:study)).to be_truthy }
it { expect(subject.respond_to?(:participant)).to be_truthy }
context 'common attributes' do
Expand Down Expand Up @@ -44,20 +45,20 @@

describe '.create!' do
context 'no participant or study' do
it { expect { described_class.create! }.to raise_error ActiveRecord::RecordInvalid }
it { expect { StudyParticipant.create! }.to raise_error ActiveRecord::RecordInvalid }
end

context 'no participant' do
it do
expect { described_class.create! participant: participant }.to \
expect { StudyParticipant.create! participant: participant }.to \
raise_error ActiveRecord::RecordInvalid
end
end

context 'no study' do
it do
expect do
described_class.create! study: study
StudyParticipant.create! study: study
end.to raise_error ActiveRecord::RecordInvalid
end
end
Expand All @@ -73,23 +74,21 @@
context 'unique study and participant' do
it do
expect do
described_class.create!(participant: participant, study: study)
StudyParticipant.create!(participant: participant, study: study)
end.to raise_error ActiveRecord::RecordInvalid
end
end
end

describe '#to_s' do
it do
expect(described_class.where(participant: participant, study: study).first.to_s).to \
expect(StudyParticipant.where(participant: participant, study: study).first.to_s).to \
eq("#{TestConstants::TEST_PARTICIPANT_EMAIL} #{TestConstants::TEST_STUDY}")
end
end

describe '#destroy!' do
before :each do
subject.destroy!
end
include_context 'destroy subject'

it { expect(Participant.exists?(participant.id)).to be_truthy }

Expand Down
4 changes: 1 addition & 3 deletions spec/models/survey_participant_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,7 @@
end

describe '#destroy!' do
before :each do
subject.destroy!
end
include_context 'destroy subject'

it { expect(Participant.exists?(participant.id)).to be_truthy }

Expand Down
8 changes: 4 additions & 4 deletions spec/rspec.txt
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ JournalEntry
unique entry date
 {:timecop=>:freeze}
#to_s
 should eq "2018-03-27 18:18:37 UTC "
 should eq "2018-03-27 18:32:05 UTC "
#created_at today
 is created less than 5 minutes ago

Expand All @@ -353,7 +353,7 @@ Journal
 should be truthy
#list_entries
defaults to last 4
 should eq "2018-03-27 18:18:37 UTC 2012-12-12 00:00:00 UTC "
 should eq "2018-03-27 18:32:05 UTC 2012-12-12 00:00:00 UTC "
#read_entry
 should be > 0
#name
Expand Down Expand Up @@ -855,7 +855,7 @@ AlexaService
 should eq " Cannot find instrument blah. Please try again.\n"
 should eq "Below is a list of the ways you might have felt or behaved. Please check the boxes to tell me how often you have felt this way in the past week or so."
#read_all
 should eq "2018-03-27 18:18:35 UTC "
 should eq "2018-03-27 18:32:04 UTC "
 should not be nil

InstrumentLoaderCsv
Expand Down Expand Up @@ -883,6 +883,6 @@ InstrumentLoader
missing instrument
 should raise RuntimeError

Finished in 7.96 seconds (files took 4.3 seconds to load)
Finished in 19.83 seconds (files took 4.36 seconds to load)
466 examples, 0 failures

6 changes: 6 additions & 0 deletions spec/shared_specs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,9 @@ def timestamps?
subject.restore_attributes
end
end

RSpec.shared_context 'destroy subject' do
before :each do
subject.destroy!
end
end

0 comments on commit 3954d8c

Please sign in to comment.