Skip to content

Latest commit

 

History

History
58 lines (48 loc) · 1.45 KB

rails_rspec.md

File metadata and controls

58 lines (48 loc) · 1.45 KB

Test after_commit callback

class Company < ActiveRecord::Base
  after_commit :create_welcome_notification, on: :create
 
  private
 
  def create_welcome_notification
    WelcomeNotificationCreator.perform_async(id)
  end
end
 
class WelcomeNotificationCreator
  include Sidekiq::Worker
 
  def perform(company_id)
    @company = Company.find(company_id)
    @company.notifications.create({ title: 'Welcome to Gusto!' })
  end
end

# rspec
# spec/models/company_spec.rb
require 'rails_helper'
 
RSpec.describe Company, type: :model do
  describe '#save' do
    subject { company.save }
    let(:company) { build(:company) }
 
    it 'schedules a WelcomeNotificationCreator job' do
      expect {
        subject
      }.to change{ WelcomeNotificationCreator.jobs.size }.by(1)
      last_job = WelcomeNotificationCreator.jobs.last          
      expect(last_job['args']).to eq([subject.id])
    end
  end
end
 
# spec/workers/welcome_notification_creator_spec.rb
require 'rails_helper'
 
RSpec.describe WelcomeNotificationCreator do
  subject { described_class.new.perform(company.id)}
  let(:company) { create(:company) }
 
  it 'creates a notification' do
    expect {
      subject
    }.to change(Notification, :count).by(1)
    expect(Notification.last.title).to eq('Welcome to Gusto!')
  end
end

source: