Skip to content

Commit

Permalink
Update discussion_spec.rb
Browse files Browse the repository at this point in the history
- update validation message spec. it is enough for us to test that discussion fails validation because there is no user. no need to check for a rails error message that has changed from 4 -> 5
- after_commit callbacks do not run in transaction fixtures in specs for Rails 4, a workaround for Rails versions less than 5 is to use run_callbacks. Issue has been fixed in Rails 5. So we add a version check on spec. See https://stackoverflow.com/a/30901628/15768801 for more details.
  • Loading branch information
yuenmichelle1 committed Jul 3, 2024
1 parent 16c1c8b commit 6a7c851
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions spec/models/discussion_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

it 'should require a user' do
without_user = build :discussion, user_id: nil
expect(without_user).to fail_validation user: "can't be blank"
expect(without_user).to fail_validation
end

it 'should require a section' do
Expand Down Expand Up @@ -193,8 +193,18 @@ def create_discussion(position: nil)
let(:discussion){ create :discussion }

it 'should queue the notification' do
expect(DiscussionSubscriptionWorker).to receive(:perform_async).with discussion.id
discussion.run_callbacks :commit
# TODO: Once on Rails 5, Can Remove this Version Check
# In Rails Versions < 5, commit callbacks are not getting called in transactional tests.
# See https://stackoverflow.com/a/30901628/15768801 for more details.
if Rails.version.starts_with?('5')
allow(DiscussionSubscriptionWorker).to receive(:perform_async)
discussion = build :discussion
discussion.save!
expect(DiscussionSubscriptionWorker).to have_received(:perform_async).with discussion.id
else
expect(DiscussionSubscriptionWorker).to receive(:perform_async).with discussion.id
discussion.run_callbacks :commit
end
end
end

Expand Down

0 comments on commit 6a7c851

Please sign in to comment.