0.0
No commit activity in last 3 years
No release in over 3 years
Defining simple scoped stub with apply/reset interface
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Development

>= 0
 Project Readme

Gem Version

SimpleStub

Defining simple scoped stub with apply/reset interface

fixed_time = Time.now
stub_time_now = SimpleStub.for_singleton_method(Time, :now) { fixed_time }

stub_time_now.apply!
# Time.now is fixed here
stub_time_now.reset!

Installation

gem 'simple_stub'

then bundle install.

Usage

For stubbing a singleton method (or class method), use SimpleStub.for_singleton_method.

fixed_time = Time.now
stub_time_now = SimpleStub.for_singleton_method(Time, :now) { fixed_time }

stub_time_now.apply!
# Time.now is fixed here
stub_time_now.reset!

For stubbing an instance method (which affects to all of the instances), use SimpleStub.for_instance_method.

class Dog
  def hello
    'Hello!'
  end
end

stub_dog_hello = SimpleStub.for_instance_method(Dog, :hello) { 'Bow' }
dog1 = Dog.new
stub_dog1_hello = SimpleStub.for_singleton_method(dog1, :hello) { ">> #{super()} <<" }

stub_dog_hello.apply!
Dog.new.hello # => 'Bow'
dog1.hello # => 'Bow'
stub_dog1_hello.apply!
dog1.hello # => '>> Bow <<'

Since SimpleStub doesn't store any state in the instance, we can use it separatedly.

class SomethingSetup
  def call
    # All username becomes John Doe
    SimpleStub.for_instance_method(User, :name) { 'John Doe' }.apply
  end
end

class SomethingTeardown
  def call
    SimpleStub.for_instance_method(User, :name).reset
  end
end

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the SimpleStub project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.