Question

To be specific, I'm trying to get ActionController::Routing::Routes.recognize_path to recognize a route that is not in routes.rb, for testing purposes.

Is it possible to somehow mock or dynamically add a route? I'm using Rspec with Mocha.

Was it helpful?

Solution

I have absolutely no idea whether will work but you could experiment with something like this:

class ApplicationController < ActionController::Base

  rescue_from ActionView::MissingTemplate do |exception|
    # use exception.path to extract the path information
    ActionController::Routing::Routes.draw do |map|
      # Add your dynamic route using path here and then do a redirect to it
    end
  end

end

OTHER TIPS

The fakeweb gem at http://github.com/chrisk/fakeweb might fit your needs.

How to register a basic string response (from the README):

FakeWeb.register_uri(:get, "http://example.com/test1", :body => "Hello World!")

To test:

Net::HTTP.get(URI.parse("http://example.com/test1"))

returns "Hello World!"

Net::HTTP.get(URI.parse("http://example.com/test2"))

In this case, FakeWeb is bypassed and the response from a real request is returned

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top