module SimpleStub
Constants
- VERSION
Public Class Methods
for_instance_method(klass, method_name, &impl)
click to toggle source
Stubs instance method. This affect to any instance of the target class.
class Dog def hello 'Hello!' end end dog1 = Dog.new stub_dog_hello = SimpleStub.for_singleton_method(Dog, :hello) { 'Bow' } stub_dog_hello.apply! dog1.hello # => 'Bow' Dog.new.hello # => 'Bow' stub_dog_hello.reset! dog1.hello # => 'Hello!' Dog.new.hello # => 'Hello!'
@param klass [Class] @param method_name [Symbol]
# File lib/simple_stub.rb, line 29 def for_instance_method(klass, method_name, &impl) ForInstanceMethod.new(klass, method_name, &impl) end
for_singleton_method(receiver, method_name, &impl)
click to toggle source
Create a definition for stubbing singleton method. This can be used to stub class method like:
fixed_time = Time.now stub_time_now = SimpleStub.for_singleton_method(Time, :now) { fixed_time } stub_time_now.apply! # Time.now returns fixed_time here. stub_time_now.reset!
And also can be used for stubbing method of an object instance.
class Dog def hello 'Hello!' end end dog1 = Dog.new stub_dog1 = SimpleStub.for_singleton_method(dog1, :hello) { 'Bow' } stub_dog1.apply! dog1.hello # => 'Bow' Dog.new.hello # => 'Hello!'
@param receiver [Class|Object] @param method_name [Symbol]
# File lib/simple_stub.rb, line 58 def for_singleton_method(receiver, method_name, &impl) ForInstanceMethod.new(receiver.singleton_class, method_name, &impl) end
Private Instance Methods
for_instance_method(klass, method_name, &impl)
click to toggle source
Stubs instance method. This affect to any instance of the target class.
class Dog def hello 'Hello!' end end dog1 = Dog.new stub_dog_hello = SimpleStub.for_singleton_method(Dog, :hello) { 'Bow' } stub_dog_hello.apply! dog1.hello # => 'Bow' Dog.new.hello # => 'Bow' stub_dog_hello.reset! dog1.hello # => 'Hello!' Dog.new.hello # => 'Hello!'
@param klass [Class] @param method_name [Symbol]
# File lib/simple_stub.rb, line 29 def for_instance_method(klass, method_name, &impl) ForInstanceMethod.new(klass, method_name, &impl) end
for_singleton_method(receiver, method_name, &impl)
click to toggle source
Create a definition for stubbing singleton method. This can be used to stub class method like:
fixed_time = Time.now stub_time_now = SimpleStub.for_singleton_method(Time, :now) { fixed_time } stub_time_now.apply! # Time.now returns fixed_time here. stub_time_now.reset!
And also can be used for stubbing method of an object instance.
class Dog def hello 'Hello!' end end dog1 = Dog.new stub_dog1 = SimpleStub.for_singleton_method(dog1, :hello) { 'Bow' } stub_dog1.apply! dog1.hello # => 'Bow' Dog.new.hello # => 'Hello!'
@param receiver [Class|Object] @param method_name [Symbol]
# File lib/simple_stub.rb, line 58 def for_singleton_method(receiver, method_name, &impl) ForInstanceMethod.new(receiver.singleton_class, method_name, &impl) end