{"id":18103961,"url":"https://github.com/svoop/minitest-substitute","last_synced_at":"2025-04-13T19:32:29.481Z","repository":{"id":190281287,"uuid":"680531306","full_name":"svoop/minitest-substitute","owner":"svoop","description":"Substitute values for the duration of a block or a group of tests.","archived":false,"fork":false,"pushed_at":"2024-12-26T19:34:34.000Z","size":150,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-06T04:17:38.009Z","etag":null,"topics":["minitest","ruby"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/svoop.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null},"funding":{"github":"svoop","custom":"https://donorbox.org/bitcetera"}},"created_at":"2023-08-19T14:49:23.000Z","updated_at":"2024-12-26T19:34:38.000Z","dependencies_parsed_at":"2023-12-29T00:26:51.625Z","dependency_job_id":"a8da06fc-c33c-4d53-8a2c-f518fdf7d308","html_url":"https://github.com/svoop/minitest-substitute","commit_stats":null,"previous_names":["svoop/minitest-substitute"],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/svoop%2Fminitest-substitute","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/svoop%2Fminitest-substitute/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/svoop%2Fminitest-substitute/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/svoop%2Fminitest-substitute/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/svoop","download_url":"https://codeload.github.com/svoop/minitest-substitute/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248767790,"owners_count":21158530,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["minitest","ruby"],"created_at":"2024-10-31T22:13:37.615Z","updated_at":"2025-04-13T19:32:29.442Z","avatar_url":"https://github.com/svoop.png","language":"Ruby","funding_links":["https://github.com/sponsors/svoop","https://donorbox.org/bitcetera"],"categories":[],"sub_categories":[],"readme":"[![Version](https://img.shields.io/gem/v/minitest-substitute.svg?style=flat)](https://rubygems.org/gems/minitest-substitute)\n[![Tests](https://img.shields.io/github/actions/workflow/status/svoop/minitest-substitute/test.yml?style=flat\u0026label=tests)](https://github.com/svoop/minitest-substitute/actions?workflow=Test)\n[![Code Climate](https://img.shields.io/codeclimate/maintainability/svoop/minitest-substitute.svg?style=flat)](https://codeclimate.com/github/svoop/minitest-substitute/)\n[![GitHub Sponsors](https://img.shields.io/github/sponsors/svoop.svg)](https://github.com/sponsors/svoop)\n\n# Minitest::Substitute\n\nSimple Minitest helper to replace values such as an instance variable of an object or an environment variable for the duration of a block or a group of tests.\n\nThis comes in very handy when you have to deviate from default configuration in order to test some aspects of your code.\n\n* [Homepage](https://github.com/svoop/minitest-substitute)\n* [API](https://www.rubydoc.info/gems/minitest-substitute)\n* Author: [Sven Schwyn - Bitcetera](https://bitcetera.com)\n\nThank you for supporting free and open-source software by sponsoring on [GitHub](https://github.com/sponsors/svoop) or on [Donorbox](https://donorbox.com/bitcetera). Any gesture is appreciated, from a single Euro for a ☕️ cup of coffee to 🍹 early retirement.\n\n## Install\n\nThis gem is [cryptographically signed](https://guides.rubygems.org/security/#using-gems) in order to assure it hasn't been tampered with. Unless already done, please add the author's public key as a trusted certificate now:\n\n```\ngem cert --add \u003c(curl -Ls https://bitcetera.com/downloads/gem-public_cert.pem)\n```\n\nAdd the following to the \u003ctt\u003eGemfile\u003c/tt\u003e or \u003ctt\u003egems.rb\u003c/tt\u003e of your [Bundler](https://bundler.io) powered Ruby project:\n\n```ruby\ngem 'minitest-substitute'\n```\n\nAnd then install the bundle:\n\n```\nbundle install --trust-policy MediumSecurity\n```\n\nFinally, require this gem in your `test_helper.rb` or `spec_helper.rb`:\n\n```ruby\nrequire 'minitest/substitute'\n```\n\n## Update from 0.x.x to 1.x.x\n\nRails 7 has polluted `Object` for everybody by introducing `Object#with`. To prevent collisions, Minitest::Substitute has switched from `with` to `substitute` as of version 1.0.0.\n\nAfter having updated this gem, you'll have to adapt all your tests accordingly:\n\n```ruby\n# Version 0.x.x\nwith '@version', 2, on: config do\n  config.instance_variable_get('@version')   # =\u003e 2\nend\n\n# Version 1.x.x\nsubstitute '@version', 2, on: config do\n  config.instance_variable_get('@version')   # =\u003e 2\nend\n```\n\n## Usage\n\n### Block\n\nTo substitute the value of an instance variable for the duration of a block:\n\n```ruby\nclass Config\n  def initialize\n    @version = 1\n  end\nend\n\nconfig = Config.new\n\nconfig.instance_variable_get('@version')     # =\u003e 1\nsubstitute '@version', 2, on: config do\n  config.instance_variable_get('@version')   # =\u003e 2\nend\nconfig.instance_variable_get('@version')     # =\u003e 1\n```\n\n:warning: The target `on` is set explicitly in this case. If you omit this argument, `self` will be used as target by default.\n\nClass variables can be substituted as well:\n\n```ruby\nclass Config\n  @@counter = 0\nend\n\nConfig.class_variable_get('@@counter')     # =\u003e 0\nsubstitute '@@counter', 42, on: Config do\n  Config.class_variable_get('@@counter')   # =\u003e 42\nend\nConfig.class_variable_get('@@counter')     # =\u003e 0\n```\n\nSame goes for global variables:\n\n```ruby\n$verbose = false   # =\u003e false\nsubstitute '$verbose', true do\n  $verbose         # =\u003e true\nend\n$verbose           # =\u003e false\n```\n\nAnd it works for globals like `ENV` as well which comes in handy when you have to temporarily override the value of an environment variable:\n\n```ruby\nENV['EDITOR']     # =\u003e 'vi'\nsubstitute \"ENV['EDITOR']\", 'nano' do\n  ENV['EDITOR']   # =\u003e 'nano'\nend\nENV['EDITOR']     # =\u003e 'vi'\n```\n\nYou can even substitute constants, however, you have to use their absolute name starting with `::`:\n\n```ruby\nmodule Animals\n  DOG_MAKES = 'woof'\n  CAT_MAKES = 'meow'\nend\n\nAnimals::DOG_MAKES     # =\u003e 'woof'\nsubstitute '::Animals::DOG_MAKES', Animals::CAT_MAKES do\n  Animals::DOG_MAKES   # =\u003e 'meow'\nend\nAnimals::DOG_MAKES     # =\u003e 'woof'\n```\n\nRemember that class declarations are assigned to constants as well:\n\n```ruby\nclass Dog\n  self.makes\n    'woof'\n  end\nend\n\nclass Cat\n  self.makes\n    'meow'\n  end\nend\n\nDog.makes     # =\u003e 'woof'\nsubstitute '::Dog', Cat do\n  Dog.makes   # =\u003e 'meow'\nend\nDog.makes     # =\u003e 'woof'\n```\n\nIt's safe to nest multiple `substitute` statements.\n\n### Group of Tests\n\nWhen using spec notation, you can change a value for all tests within a `describe` group:\n\n```ruby\nclass Config\n  def initialize\n    @version = 1\n  end\nend\n\ndescribe Config do\n  subject do\n    Config.new\n  end\n\n  describe 'original version' do\n    it \"returns the original version\" do\n      _(subject.instance_variable_get('@version')).must_equal 1\n    end\n  end\n\n  describe 'sustituted version' do\n    substitute '@version', 2, on: Config\n\n    it \"returns the substituted version\" do\n      _(subject.instance_variable_get('@version')).must_equal 2\n    end\n  end\nend\n```\n\n:warning: The target `on` is set explicitly in this case. If you omit this argument, `:subject` will be used as target by default which refers to the subject defined by the `subject {}` helper.\n\nAlternatively, you can pass the substitution value as a block. This block will be evaluated once in the context of the test, in other words, you can use assignments done with `let` inside the block:\n\n```ruby\nclass Config\n  def initialize\n    @version = 1\n  end\nend\n\ndescribe Config do\n  subject do\n    Config.new\n  end\n\n  let :version do\n    2\n  end\n\n  describe 'original version' do\n    it \"returns the original version\" do\n      _(subject.instance_variable_get('@version')).must_equal 1\n    end\n  end\n\n  describe 'sustituted version' do\n    substitute '@version', on: Config do\n      version   # set using \"let\" above\n    end\n\n    it \"returns the substituted version\" do\n      _(subject.instance_variable_get('@version')).must_equal 2\n    end\n  end\nend\n```\n\nIf both a substitution value and a substitution block are present, the latter takes precedence.\n\nIt's safe to use multiple `substitute` statements within one `describe` block.\n\n(The spec integration is borrowed from [minitest-around](https://rubygems.org/gems/minitest-around) for elegance and compatibility.)\n\n## Development\n\nTo install the development dependencies and then run the test suite:\n\n```\nbundle install\nbundle exec rake    # run tests once\nbundle exec guard   # run tests whenever files are modified\n```\n\nYou're welcome to [submit issues](https://github.com/svoop/minitest-substitute/issues) and contribute code by [forking the project and submitting pull requests](https://docs.github.com/en/get-started/quickstart/fork-a-repo).\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsvoop%2Fminitest-substitute","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsvoop%2Fminitest-substitute","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsvoop%2Fminitest-substitute/lists"}