Skip to content

Commit

Permalink
Rails 5 - Fix discussion_spec.rb (#341)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
yuenmichelle1 authored Jul 8, 2024
1 parent 569f9fe commit 3c0bca3
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 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 @@ -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

Expand Down

0 comments on commit 3c0bca3

Please sign in to comment.