Skip to content

Instantly share code, notes, and snippets.

@cpruitt
Last active November 6, 2020 16:20
Show Gist options
  • Select an option

  • Save cpruitt/31ec22b4748cd29d24e11d7bded49b2f to your computer and use it in GitHub Desktop.

Select an option

Save cpruitt/31ec22b4748cd29d24e11d7bded49b2f to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem "rails", "6.1.0.rc1"
end
require "rails/all"
require "minitest/autorun"
#===== Framework code that raises deprecation warnings
module MessagingFramework
def send_sms(message, recipient_phone:, sender_phone: nil)
unless sender_phone
ActiveSupport::Deprecation.warn("Calling send_sms without a sender_phone value is deprecated and will not be allowed in future versions")
end
# Do some SMS sending stuff...
true
end
end
#===== Initializer configuration that treats the deprecation as a failure
ActiveSupport::Deprecation.disallowed_warnings = ["Calling send_sms"]
#===== App code that uses deprecated code
class User
attr_accessor :phone
include MessagingFramework
def initialize(phone: nil)
self.phone = phone
end
def contact_by_sms(message, sender_phone:)
send_sms(message, recipient_phone: phone, sender_phone: sender_phone)
end
def contact_by_sms_from_anonymous_sender(message)
send_sms(message, recipient_phone: phone)
end
end
#===== Tests using deprecated code
#
# Example of a test that will raise an exception and a test that temporarily
# re-allows the deprecation warning to provide time to come up with a fix
class TestUserContact < Minitest::Test
def test_sms_send_with_missing_sender
user = User.new(phone: "555-123-4567")
# This will raise an ActiveSupport::DeprecationException
assert user.contact_by_sms("Thank you for your reservation.", sender_phone: nil)
end
def test_sms_send_with_anonymous_sender
user = User.new(phone: "555-123-4567")
# This will print a warning instead of raising an exception
ActiveSupport::Deprecation.allow ["Calling send_sms"] do
assert user.contact_by_sms_from_anonymous_sender("Thank you for your reservation.")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment