Skip to content

Instantly share code, notes, and snippets.

@ujjwalt
Last active December 22, 2015 23:49
Show Gist options
  • Save ujjwalt/6549756 to your computer and use it in GitHub Desktop.
Save ujjwalt/6549756 to your computer and use it in GitHub Desktop.
My valiant attempt at writing a routing test - have no clue how to write one
require 'abstract_unit'
require 'controller/fake_controllers'
class TestCollectionRouting < ActionDispatch::IntegrationTest
test "collection option" do
with_routing do |set|
set.draw do
resources :posts, collection: true
end
assert_routing "/posts/1", {controller: "posts", action: "show", id: "1"}
end
end
end
@pixeltrix
Copy link

Don't use assert_routing - it only works in functional tests.

Here's a skeleton that you can use:

require 'abstract_unit'

class TestCollectionRouting < ActionDispatch::IntegrationTest
  test 'collection option' do
    draw do
      resources :posts, collection: true
    end

    get '/posts/1'
    assert_equal 'posts#show', @response.body
    assert_equal '/posts/1', post_path(id: '1')
  end

  private

    def draw(&block)
      self.class.stub_controllers do |routes|
        @app = routes
        @app.default_url_options = { host: 'www.example.com' }
        @app.draw(&block)
      end
    end

    def url_for(options = {})
      @app.url_helpers.url_for(options)
    end

    def method_missing(method, *args, &block)
      if method.to_s =~ /_(path|url)$/
        @app.url_helpers.send(method, *args, &block)
      else
        super
      end
    end

end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment