Module: Greenbar::ClassMethodSetup
Instance Method Summary collapse
-
#define_class_method(target_class, method_name, &block) ⇒ Object
Defines a new class method method_name on class target_class.
-
#replace_class_method(target_class, method_name, &block) ⇒ Object
Replaces a class method method_name on class target_class.
-
#replace_new(target_class, results) ⇒ Object
Replaces the new method on class target_class.
-
#setup_mixin ⇒ Object
:nodoc:.
-
#teardown_mixin ⇒ Object
:nodoc:.
Methods included from TestSetup
included, #setup, #setup_mixins, #teardown, #teardown_mixins
Instance Method Details
#define_class_method(target_class, method_name, &block) ⇒ Object
Defines a new class method method_name on class target_class. The implementation will be the block, and may take any number of arguments. The method will be destroyed automatically at the end of the test. The primary purpose for this method is when you need to provide a mock implementation of a method that is usually handled by method_missing implementation
100 101 102 103 |
# File 'lib/greenbar/ClassMethodSetup.rb', line 100 def define_class_method target_class, method_name, &block @class_methods_to_remove << [target_class, method_name] target_class..define_method method_name, &block end |
#replace_class_method(target_class, method_name, &block) ⇒ Object
Replaces a class method method_name on class target_class. The implementation will be the block, and may take any number of arguments. The original method will be restored automatically. The primary purpose for this method is when you need to provide a mock implementation of a class method.
87 88 89 90 91 |
# File 'lib/greenbar/ClassMethodSetup.rb', line 87 def replace_class_method target_class, method_name, &block @class_methods_to_restore << [target_class, method_name] target_class.alias_class_method name_of_copied_method(method_name), method_name target_class..define_method method_name, &block end |
#replace_new(target_class, results) ⇒ Object
Replaces the new method on class target_class.
In the canonical form, results should respond to to_hash. The hash is keyed by arrays that match the expected arguments to the new method. The hash value is the object to return for that set of arguments.
If results does not respond to to_hash, it provides the desired return value for a no-argument call to new. That is, it is shorthand for => results.
114 115 116 117 118 119 120 121 |
# File 'lib/greenbar/ClassMethodSetup.rb', line 114 def replace_new target_class, results argument_instance_map = results.respond_to?(:to_hash) ? results.to_hash : { [] => results } replace_class_method(target_class, :new) {|*args| return argument_instance_map[args] if argument_instance_map.has_key? args raise "No instance has been set to return from #{target_class}.new for #{args.inspect}." } end |
#setup_mixin ⇒ Object
:nodoc:
67 68 69 70 |
# File 'lib/greenbar/ClassMethodSetup.rb', line 67 def setup_mixin #:nodoc: @class_methods_to_restore = [] @class_methods_to_remove = [] end |
#teardown_mixin ⇒ Object
:nodoc:
72 73 74 75 76 77 78 79 80 |
# File 'lib/greenbar/ClassMethodSetup.rb', line 72 def teardown_mixin #:nodoc: @class_methods_to_restore.each {|target_class, method_name| target_class.alias_class_method method_name, name_of_copied_method(method_name) } @class_methods_to_remove.each {|target_class, method_name| target_class.remove_class_method method_name } end |