From 3c0bca3f7b9b9ed1b5af57d2cc27844d2bf54929 Mon Sep 17 00:00:00 2001 From: yuenmichelle1 Date: Mon, 8 Jul 2024 09:06:45 -0500 Subject: [PATCH] Rails 5 - Fix discussion_spec.rb (#341) * Update discussion_spec.rb - 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. * Update discussion_spec.rb --- spec/models/discussion_spec.rb | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/spec/models/discussion_spec.rb b/spec/models/discussion_spec.rb index 9c1f2b97..0c4b3600 100644 --- a/spec/models/discussion_spec.rb +++ b/spec/models/discussion_spec.rb @@ -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 @@ -190,11 +190,20 @@ def create_discussion(position: nil) end describe '#notify_subscribers_later' do - 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 + discussion = create :discussion + expect(DiscussionSubscriptionWorker).to receive(:perform_async).with discussion.id + discussion.run_callbacks :commit + end end end