Skip to content

Instantly share code, notes, and snippets.

@damien
Last active April 28, 2021 12:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save damien/af43487dbb4b9d57e69a to your computer and use it in GitHub Desktop.
Save damien/af43487dbb4b9d57e69a to your computer and use it in GitHub Desktop.
Using MiniTest to mock and test ActiveRecord callbacks in Rails 4.2
class TeamMembership < ActiveRecord::Base
# A proc that will enqueue `NotificationMailer.team_invitation`
DEFAULT_NOTIFIER = proc do |user, team|
NotificationMailer.team_invitation(team, user).deliver_later
end
class << self
# This is a class level attribute that is mainly used for testing.
# Defaults to {TeamMembership::DEFAULT_NOTIFIER}
attr_accessor :notifier
end
self.notifier = DEFAULT_NOTIFIER
belongs_to :team
belongs_to :user
after_create :invite_user_to_team!
private
# ActiveRecord callback used to equeue team invitation emails
# @return void
def invite_user_to_team!
self.class.notifier.call(team, user)
nil
end
end
require 'test_helper'
class TeamMemberTest < ActiveSupport::TestCase
test 'callbacks' do
# setup
test_notifier = Minitest::Mock.new
test_notifier.expect(:call, nil, [teams(:alpha), users(:carol)])
TeamMembership.notifier = test_notifier
# test
TeamMembership.create(user_id: users(:carol).id, team_id: teams(:alpha).id)
assert test_notifier.verify
# teardown
TeamMembership.notifier = TeamMembership::DEFAULT_NOTIFIER
end
end
@benkoshy
Copy link

Thanks for posting.

I wonder if there is a better way than adding a class level modifier to simply test a call back? Something about this approach doesn't feel quite right, would be interested if anyone has any ideas?

@damien
Copy link
Author

damien commented Apr 27, 2021

@benkoshy This was written for Rails cira version 4.2 A lot has changed since then, so I would not be surprised if there are better ways of testing/asserting desired behavior for stuff like this.

Looking at Rails Guides: ActiveRecord Callbacks: 9. Callback Classes, if I were to do this today I'd write a tests around an AR callback class and test that independently of the AR model it was used in. Depending on your needs, your AR callback class could take arguents on it's initialization method if you needed to make it configurable or wanted to pass in mocks during testing:

# AR Model
class TeamMembership < ActiveRecord::Base
  belongs_to :team
  belongs_to :user

  after_create EmailNotificationCallback.new( NotificationMailer.method(:team_invitation) )
end

# AR Callback
class EmailNotificationCallback
  def initialize(mailer_proc, **mailer_args)
    @mailer = mailer_proc
    @mailer_args = mailer_args
  end

  def after_create(membership)
    mailer.call(membership.user, membership.team)
  end
end

# AR Model Callback Test
# All we need to do is verify that our callback class is called
class TeamMembershipTest < ActiveSupport::TestCase
  test 'callbacks' do
    # setup
    test_callback = Minitest::Mock.new
    test_callback.expect(:new, nil, [teams(:alpha), users(:carol)])

    # test
    EmailNotificationCallback.stub do
      TeamMembership.create(user_id: users(:carol).id, team_id: teams(:alpha).id)
    end

    assert test_callback.verify # Assert that our AR Callback was executed
  end
end

# AR Callback Test
# We can now test the callback in isolation since we've verified the model <-> callback hook up in a previous test
class EmailNotificationCallback < ActiveSupport::TestCase
  test 'after_create' do
    # setup
    mailer_proc = Minitest::Mock.new
    mailer_proc.expect(:call, nil, [teams(:alpha), users(:carol)])

    membership = TeamMembership.create(user_id: users(:carol).id, team_id: teams(:alpha).id)

    instance = EmailNotificationCallback.new(mailer_proc)

    # test
    instance.after_create(membership)
    mailer_proc.verify # assert our mailer gets called with expected arguments
  end
end

Note: Code is untested, but should demonstrate my thoughts well enough.

@benkoshy
Copy link

@damien, i appreciate the suggestions. I will implement as you've suggested. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment