Giter VIP home page Giter VIP logo

rails_anonymous_controller_testing's Introduction

Rails Anonymous Controller Testing

I sometimes want to test an abstract controller, a controller concern, a view, or a helper method.

This might be useful for you if you're working on a Rails engine or if you're editing your ApplicationController.

This only works with Minitest, specifically for tests that inherit from ActionDispatch::IntegrationTest.

Usage

Here's a relatively straightforward example. There are a few caveats you might need to keep in mind for more complicated cases; see the Caveats section below.

class MyControllerTest < ActionDispatch::IntegrationTest
  # 1. Call the `controller` method. You must specify the base controller to
  #    inherit from. It'll set up routes with `resources :anonymous`.
  controller(ApplicationController) do
    def index
    end

    def show
      render plain: params[:id]
    end
  end

  # 2. Optionally, set views. You can set the layout for your anonymous
  #    controller too.
  views["layouts/application.html.erb"] = <<~HTML
    <h1>My anonymous test</h1>
    <%= yield %>
  HTML

  views["index.html.erb"] = <<~HTML
    <h2>Hi</h2>
  HTML

  # 3. Test like it's a regular controller in your application
  def test_index
    get "/anonymous"
    assert_select "h1", "My anonymous test"
    assert_select "h2", "Hi"
  end

  def test_show
    get "/anonymous/1234"
    assert_equal "1234", response.body
  end
end

You can also specify the routes too, but you need to namespace the controller to the name of the test class.

class MyControllerTest < ActionDispatch::IntegrationTest
  routes = -> { get "custom", to: "my_controller_test/anonymous#custom" }
  controller(ApplicationController, routes: routes) do
    def custom
    end
  end
end

Caveats

A few things to note

  • You can only define one controller do per test class. But you can get around this limitation by defining another test class in one file.
  • Views are generated only once and cached. The cache key is determined by MD5 hashing the test file contents, and test file path, and the line number of the controller do.
  • You can disable the view cache by calling disable_anonymous_view_cache! on your test class. This doesn't get around the limitation of having just one controller do definition in your test class.
  • The view cache is located in tmp/anonymous_controller_views and can be cleared anytime
  • Setting a custom layout will only affect your anonymous controller. This gem does not provide any way to affect the behavior of existing controllers defined by your Rails application or engine

Installation

Add this line to your application's Gemfile:

gem "rails_anonymous_controller_testing", group: :test

And then execute:

$ bundle

Contributing

Contributions very welcome. Please give me edit access to your branch for a faster turnaround.

License

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

rails_anonymous_controller_testing's People

Contributors

zachahn avatar dependabot[bot] avatar miguelperez avatar

Stargazers

Cristian Molina avatar  avatar Weston Ganger avatar

Watchers

 avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.