New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

simple_stub

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

simple_stub

  • 0.1.0
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

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.

FAQs

Package last updated on 20 Sep 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc