This was a really neat way of monkey patching method definitions in our ruby tests, without leaving any side effects.
require 'test_helper'
class ComplexTest < ActiveSupport::TestCase
describe 'with an API call half a second later' do
before do
Complex.singleton_class.send(:alias_method, :old_api, :api)
Complex.send(:define_singleton_method, :api) do |_arg1|
sleep(0.5)
OpenStruct.new(status: 'succeeded'])
end
it 'succeeds' do
assert Complex.operation
end
after do
Complex.singleton_class.send(:alias_method, :api, :old_api)
end
end
end
# inside our complex.rb file, it remains the same
module Complex
def self.api(argument1)
# do fancy stuff
end
def self.operation
# more stuff
api(arg1)
end
end
Top comments (0)