{"id":24461798,"url":"https://github.com/zachahn/rails_anonymous_controller_testing","last_synced_at":"2025-11-11T19:30:58.648Z","repository":{"id":39915746,"uuid":"391635195","full_name":"zachahn/rails_anonymous_controller_testing","owner":"zachahn","description":" Define anonymous controllers in your Rails integration tests ","archived":false,"fork":false,"pushed_at":"2023-03-09T00:55:19.000Z","size":71,"stargazers_count":3,"open_issues_count":5,"forks_count":2,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-05-15T10:42:37.000Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/zachahn.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","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,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2021-08-01T13:30:20.000Z","updated_at":"2024-06-29T23:07:05.000Z","dependencies_parsed_at":"2025-04-14T12:32:18.526Z","dependency_job_id":null,"html_url":"https://github.com/zachahn/rails_anonymous_controller_testing","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/zachahn/rails_anonymous_controller_testing","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zachahn%2Frails_anonymous_controller_testing","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zachahn%2Frails_anonymous_controller_testing/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zachahn%2Frails_anonymous_controller_testing/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zachahn%2Frails_anonymous_controller_testing/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zachahn","download_url":"https://codeload.github.com/zachahn/rails_anonymous_controller_testing/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zachahn%2Frails_anonymous_controller_testing/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267318622,"owners_count":24068486,"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","status":"online","status_checked_at":"2025-07-27T02:00:11.917Z","response_time":82,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":[],"created_at":"2025-01-21T04:29:48.765Z","updated_at":"2025-11-11T19:30:53.608Z","avatar_url":"https://github.com/zachahn.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Rails Anonymous Controller Testing\n\nI sometimes want to test an abstract controller, a controller concern, a view,\nor a helper method.\n\nThis might be useful for you if you're working on a Rails engine or if you're\nediting your `ApplicationController`.\n\nThis only works with Minitest, specifically for tests that inherit from\n`ActionDispatch::IntegrationTest`.\n\n\n## Usage\n\nHere's a relatively straightforward example. There are a few caveats you might\nneed to keep in mind for more complicated cases; see the Caveats section below.\n\n```ruby\nclass MyControllerTest \u003c ActionDispatch::IntegrationTest\n  # 1. Call the `controller` method. You must specify the base controller to\n  #    inherit from. It'll set up routes with `resources :anonymous`.\n  controller(ApplicationController) do\n    def index\n    end\n\n    def show\n      render plain: params[:id]\n    end\n  end\n\n  # 2. Optionally, set views. You can set the layout for your anonymous\n  #    controller too.\n  views[\"layouts/application.html.erb\"] = \u003c\u003c~HTML\n    \u003ch1\u003eMy anonymous test\u003c/h1\u003e\n    \u003c%= yield %\u003e\n  HTML\n\n  views[\"index.html.erb\"] = \u003c\u003c~HTML\n    \u003ch2\u003eHi\u003c/h2\u003e\n  HTML\n\n  # 3. Test like it's a regular controller in your application\n  def test_index\n    get \"/anonymous\"\n    assert_select \"h1\", \"My anonymous test\"\n    assert_select \"h2\", \"Hi\"\n  end\n\n  def test_show\n    get \"/anonymous/1234\"\n    assert_equal \"1234\", response.body\n  end\nend\n```\n\nYou can also specify the routes too, but you need to namespace the controller to\nthe name of the test class.\n\n```ruby\nclass MyControllerTest \u003c ActionDispatch::IntegrationTest\n  routes = -\u003e { get \"custom\", to: \"my_controller_test/anonymous#custom\" }\n  controller(ApplicationController, routes: routes) do\n    def custom\n    end\n  end\nend\n```\n\n\n### Caveats\n\nA few things to note\n\n* You can only define **one** `controller do` per test class. But you can get\n  around this limitation by defining another test class in one file.\n* Views are generated only once and cached. The cache key is determined by MD5\n  hashing the test file contents, and test file path, and the line number of the\n  `controller do`.\n* You can disable the view cache by calling `disable_anonymous_view_cache!` on\n  your test class. This doesn't get around the limitation of having just one\n  `controller do` definition in your test class.\n* The view cache is located in `tmp/anonymous_controller_views` and can be\n  cleared anytime\n* Setting a custom layout will only affect your anonymous controller. This gem\n  does not provide any way to affect the behavior of existing controllers\n  defined by your Rails application or engine\n\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem \"rails_anonymous_controller_testing\", group: :test\n```\n\nAnd then execute:\n\n```bash\n$ bundle\n```\n\n\n## Contributing\n\nContributions very welcome. Please give me edit access to your branch for a\nfaster turnaround.\n\n\n## License\n\nThe gem is available as open source under the terms of the\n[MIT License](https://opensource.org/licenses/MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzachahn%2Frails_anonymous_controller_testing","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzachahn%2Frails_anonymous_controller_testing","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzachahn%2Frails_anonymous_controller_testing/lists"}