Skip to content

Instantly share code, notes, and snippets.

@kyletolle
Last active January 19, 2016 21:02
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 kyletolle/2e3214ef155e141c7f23 to your computer and use it in GitHub Desktop.
Save kyletolle/2e3214ef155e141c7f23 to your computer and use it in GitHub Desktop.
RSpec alias_method matcher
# spec/support/matchers/alias_method_matcher.rb
# RSpec matcher for alias_method.
# Orignal idea borrowed from: https://gist.github.com/1950961
# Usage:
#
# describe Boomerang do
# let(:boomerang) { described_class.new }
# let(:subject} { boomerang }
# it { is_expected.to alias_method(:in_flight?).to(:in_air?) }
# end
RSpec::Matchers.define :alias_method do |alias_method|
match do |subject|
begin
subject.send(alias_method)
rescue NoMethodError
raise "expected alias_method from #{alias_method} to #{@original_method} but #{alias_method} is not defined"
end
expect(subject.method(alias_method)).to eq(subject.method(@original_method))
end
description do
"RSpec matcher for alias_method"
end
failure_message do |text|
"expected alias_method from #{alias_method} to #{@original_method}"
end
failure_message_when_negated do |text|
"do not expected alias_method from #{alias_method} to #{@original_method}"
end
chain(:to) { |original_method| @original_method = original_method }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment