{"id":14991278,"url":"https://github.com/zaratan/rspec_in_context","last_synced_at":"2025-04-12T03:41:42.010Z","repository":{"id":55045113,"uuid":"145402128","full_name":"zaratan/rspec_in_context","owner":"zaratan","description":"This gem is here to help you write better shared_examples in Rspec.","archived":false,"fork":false,"pushed_at":"2021-01-20T01:31:38.000Z","size":71,"stargazers_count":11,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-06T15:04:47.538Z","etag":null,"topics":["rspec","ruby"],"latest_commit_sha":null,"homepage":"https://rubygems.org/gems/rspec_in_context","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zaratan.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-08-20T10:20:22.000Z","updated_at":"2023-07-11T20:11:43.000Z","dependencies_parsed_at":"2022-08-14T10:00:47.030Z","dependency_job_id":null,"html_url":"https://github.com/zaratan/rspec_in_context","commit_stats":null,"previous_names":["denispasin/rspec_in_context"],"tags_count":19,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zaratan%2Frspec_in_context","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zaratan%2Frspec_in_context/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zaratan%2Frspec_in_context/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zaratan%2Frspec_in_context/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zaratan","download_url":"https://codeload.github.com/zaratan/rspec_in_context/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248514221,"owners_count":21116899,"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":["rspec","ruby"],"created_at":"2024-09-24T14:22:05.622Z","updated_at":"2025-04-12T03:41:41.990Z","avatar_url":"https://github.com/zaratan.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# RspecInContext\n\n[![Gem Version](https://badge.fury.io/rb/rspec_in_context.svg)](https://badge.fury.io/rb/rspec_in_context)\n[![Codacy Badge](https://app.codacy.com/project/badge/Grade/6490834b08664dc898d0107c74a78357)](https://www.codacy.com/gh/zaratan/rspec_in_context/dashboard?utm_source=github.com\u0026amp;utm_medium=referral\u0026amp;utm_content=zaratan/rspec_in_context\u0026amp;utm_campaign=Badge_Grade)\n![Test and Release badge](https://github.com/zaratan/rspec_in_context/workflows/Test%20and%20Release/badge.svg)\n\nThis gem is here to help you write better shared_examples in Rspec.\n\nEver been bothered by the fact that they don't really behave like methods and that you can't pass it a block ? There you go: `rspec_in_context`\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'rspec_in_context'\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install rspec_in_context\n\n## Usage\n\n### Add this into Rspec\n\nYou must require the gem on top of your spec_helper:\n```ruby\nrequire 'rspec_in_context'\n```\n\nThen include it into Rspec:\n```ruby\nRSpec.configure do |config|\n  [...]\n  \n  config.include RspecInContext\nend\n```\n\n### Define a new in_context\n\nYou can define in_context block that are reusable almost anywhere.\nThey completely look like normal Rspec.\n\n##### Inside a Rspec block (scoped)\n\n```ruby\n# A in_context can be named with a symbol or a string\ndefine_context :context_name do\n  it 'works' do\n    expect(true).to be_truthy\n  end\nend\n```\n\nThose in_context will be scoped to their current `describe`/`context` block.\n\n##### Outside a Rspec block (globally)\n\nOutside of a test you have to use `RSpec.define_context`. Those in_context will be defined globally in your tests.\n\n\n### Use the context\n\nAnywhere in your test description, use a `in_context` block to use a predefined in_context.\n\n**Important**: in_context are scoped to their current `describe`/`context` block. If you need globally defined context see `RSpec.define_context`\n\n```ruby\n# A in_context can be named with a symbol or a string\nRSpec.define_context :context_name do\n  it 'works' do\n    expect(true).to be_truthy\n  end\nend\n\n[...]\nRspec.describe MyClass do\n  in_context :context_name # =\u003e will execute the 'it works' test here\nend\n```\n\n### Things to know\n\n#### Inside block execution\n\n* You can chose exactly where your inside test will be used:\nBy using `execute_tests` in your define context, the test passed when you *use* the context will be executed here\n\n```ruby\ndefine_context :context_name do\n  it 'works' do\n    expect(true).to be_truthy\n  end\n  context \"in this context pomme exists\" do\n    let(:pomme) { \"abcd\" }\n    \n    execute_tests\n  end\nend\n\n[...]\n\nin_context :context_name do\n  it 'will be executed at execute_tests place' do\n    expect(pomme).to eq(\"abcd\") # =\u003e true\n  end\nend\n```\n\n* You can add variable instantiation relative to your test where you exactly want:\n\n`instanciate_context` is an alias of `execute_tests` so you can't use both.\nBut it let you describe what the block will do better.\n\n#### Variable usage\n\n* You can use variable in the in_context definition\n\n```ruby\ndefine_context :context_name do |name|\n  it 'works' do\n    expect(true).to be_truthy\n  end\n  context \"in this context #{name} exists\" do\n    let(name) { \"abcd\" }\n    \n    execute_tests\n  end\nend\n\n[...]\n\nin_context :context_name, :poire do\n  it 'the right variable will exists' do\n    expect(poire).to eq(\"abcd\") # =\u003e true\n  end\nend\n```\n\n#### Scoping\n\n* In_contexts can be scope inside one another\n\n```ruby\ndefine_context :context_name do |name|\n  it 'works' do\n    expect(true).to be_truthy\n  end\n  context \"in this context #{name} exists\" do\n    let(name) { \"abcd\" }\n    \n    execute_tests\n  end\nend\n\ndefine_context \"second in_context\" do\n  context 'and tree also' do\n    let(:tree) { 'abcd' }\n\n    it 'will scope correctly' do\n      expect(tree).to eq(poire)\n    end\n  end\nend\n\n[...]\n\nin_context :context_name, :poire do\n  it 'the right variable will exists' do\n    expect(poire).to eq(\"abcd\") # =\u003e true\n  end\n\n  in_context \"second in_context\" # =\u003e will work\nend\n```\n\n* in_context are bound to their current scope\n\n#### Namespacing\n\n* You can add a namespace to a in_context definition\n\n```ruby\ndefine_context \"this is a namespaced context\", namespace: \"namespace name\"\n```\nOr\n```ruby\ndefine_context \"this is a namespaced context\", ns: \"namespace name\"\n```\nOr\n```ruby\nRSpec.define_context \"this is a namespaced context\", ns: \"namespace name\"\n```\n\n* When you want to use a namespaced in_context, you have two choice:\n\nIgnore any namespace and it will try to find a corresponding in_context in any_namespace (the ones defined without namespace have the priority);\n```ruby\ndefine_context \"namespaced context\", ns: \"namespace name\" do\n  [...]\nend\n\nin_context \"namespaced context\"\n```\n\nPass a namespace and it will look only in this context.\n```ruby\ndefine_context \"namespaced context\", ns: \"namespace name\" do\n  [...]\nend\n\nin_context \"namespaced context\", namespace: \"namespace name\"\nin_context \"namespaced context\", ns: \"namespace name\"\n```\n\n#### Making `in_context` adverstise itself\n\nThe fact that a `in_context` block is used inside the test is silent and invisible by default.\n`in_context` will still wrap its own execution inside a anonymous context.\n\nBut, there's some case where it helps to make the `in_context` to wrap its execution in a named `context` block.\nFor example:\n```ruby\ndefine_context \"with my_var defined\" do\n  before do\n    described_class.set_my_var(true)\n  end\n\n  it \"works\"\nend\n\ndefine_context \"without my_var defined\" do\n  it \"doesn't work\"\nend\n\nRSpec.describe MyNiceClass do\n  in_context \"with my_var defined\"\n  in_context \"without my_var defined\"\nend\n```\nUsing a `rspec -f doc` will only print \"MyNiceClass works\" and \"MyNiceClass doesn't work\" which is not really a good documentation.\n\nSo, you can define a context specifying it not to be `silent` or to `print_context`.\nFor example :\n```ruby\ndefine_context \"with my_var defined\", silent: false do\n  before do\n    described_class.set_my_var(true)\n  end\n\n  it \"works\"\nend\n\ndefine_context \"without my_var defined\", print_context: true do\n  it \"doesn't work\"\nend\n\nRSpec.describe MyNiceClass do\n  in_context \"with my_var defined\"\n  in_context \"without my_var defined\"\nend\n```\nWill print \"MyNiceClass with my_var defined works\" and \"MyNiceClass without my_var defined doesn't work\". Which is valid and readable documentation.\n\n## Development\n\n\nAfter checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.\n\nAfter setuping the repo, you should run `overcommit --install` to install the different hooks.\n\nTo install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/denispasin/rspec_in_context.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzaratan%2Frspec_in_context","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzaratan%2Frspec_in_context","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzaratan%2Frspec_in_context/lists"}