<< | Index | >>
To replace a constant in a method
keep = ODDB::Doctors::DoctorPlugin::PARSER eval 'ODDB::Doctors::DoctorPlugin::PARSER = xxx' assert_equal(something) eval 'ODDB::Doctors::DoctorPlugin::PARSER = keep'
Note
This is useful to avoid outputting warning message
def stderr_null
require 'tempfile'
$stderr = Tempfile.open('stderr')
yield
$stderr.close
$stderr = STDERR
end
def replace_constant(constant, temp)
stderr_null do
keep = eval constant
eval "#{constant} = temp"
yield
eval "#{constant} = keep"
end
end
Example
module ODDB
module Doctors
class DoctorPlugin < Plugin
RECIPIENTS = []
PARSER = DRbObject.new(nil, DOCPARSE_URI)
...
def get_doctor_data(doc_id)
retry_count = 3
begin
self::class::PARSER.doc_data_add_ean(doc_id)
...
def test_get_doctor_data
parser = flexmock('parser', :doc_data_add_ean => 'doc_data_add_ean')
replace_constant('ODDB::Doctors::DoctorPlugin::PARSER', parser) do
assert_equal('doc_data_add_ean', @plugin.get_doctor_data('doc_id'))
end
end