Last active
April 28, 2021 12:50
-
-
Save damien/af43487dbb4b9d57e69a to your computer and use it in GitHub Desktop.
Using MiniTest to mock and test ActiveRecord callbacks in Rails 4.2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@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:
Note: Code is untested, but should demonstrate my thoughts well enough.