Plugin controller spec: Plugin routes are not drawn

Running the tests for my plugin like this:

LOAD_PLUGINS=1 bundle exec rake plugin:spec["my-plugin"]

… the plugin’s routes don’t seem to be available in the following spec file:

spec/controllers/plugin/routes_controller_spec.rb:

require 'rails_helper'

describe Plugin::RoutesController do
  it 'should route to #index' do
    expect(get '/plugin_routes').to route_to(action: :index, controler: 'plugin/routes')
  end
end

Error:

ActionController::UrlGenerationError:
No route matches {:action=>"/plugin_routes", :controller=>"plugin/routes"}

What am I doing wrong? Below are excerpts of my plugin’s code:

plugin.rb:

# …

Discourse::Application.routes.append do
  get '/plugin_routes' => 'plugin/routes#index', constraints: AdminConstraint.new
end

app/controllers/plugin/routes_controller.rb:

class Plugin::RoutesController < ApplicationController
  def index
    # …
  end
end

I’m not sure what you’re doing wrong from your example code, but we have plugins that draw routes and then test them:

https://github.com/discourse/discourse-akismet/blob/master/plugin.rb#L77-L79

and

https://github.com/discourse/discourse-akismet/blob/master/spec/requests/admin_mod_queue_controller_spec.rb#L14

I would try to pare down your code until it works.

6 Likes

I have my plugin’s routes set up like this:

Discourse::Application.routes.append do
  get '/plugin_routes' => 'plugin/routes#index', constraints: AdminConstraint.new
  put '/plugin_routes/:route_id' => 'plugin/routes#update', constraints: AdminConstraint.new
  delete '/plugin_routes/:route_id' => 'plugin/routes#destroy', constraints: AdminConstraint.new
end

I’m not using an engine. Does that matter?

If you are creating a sub-path for your plugin, I recommend an engine. All our major plugins do so. If you are wanting to change existing routes, an engine is not required.

I don’t see anything immediately wrong with your example. My suggestion would be to start with one of the official plugins and update it to match what you are doing until it works, and go from there.

2 Likes

I tried using routes, but couldn’t figure out why the routes aren’t drawn with this setup:

Plugin::Engine.routes.draw do
  get '/plugin_routes' => 'plugin/routes#index'
  put '/plugin_routes/:route_id' => 'plugin/routes#update'
  delete '/plugin_routes/:route_id' => 'plugin/routes#destroy'
end

Discourse::Application.routes.append do
  mount Plugin::Engine, at: '/plugin', constraints: AdminConstraint.new
end

Running rake routes lists the engine, but no entries under Routes for Plugin::Engine:. The routes are listed for the poll plugin, for example. I can’t tell what the difference between my plugin and other plugins is which use engines.

Again, please download one of our existing plugins and start removing code / modifying routes until it matches what you want. That is the best advice I can give.

3 Likes

For who might experience the same problem as described above, one way I am able to get the routes drawn with an engine was defining the engine inside plugin.rb rather than lib/engine.rb.

after_initialize do
  # …

  module ::Plugin
    class Engine < ::Rails::Engine
      engine_name Plugin::PLUGIN_NAME
      isolate_namespace Plugin
    end
  end

  Plugin::Engine.routes.draw do
    get '/my_routes' => 'routes#index'
    put '/my_routes/:route_id' => 'routes#update'
    delete '/my_routes/:route_id' => 'routes#destroy'
  end

  Discourse::Application.routes.append do
    mount Plugin::Engine, at: '/plugin', constraints: AdminConstraint.new
  end
end

Why that makes any difference is beyond me.


As for the strategy that was suggested: Yes, I could certainly try re-implementing my plugin starting with one of the default plugins and observe at which point the described failure occurs. However, that’s very impractical as I’ve working on the plugin for close to a year.

1 Like

There’s no need to use the LOAD_PLUGINS=1 in this case since the plugin:spec rake task already does it :wink:

4 Likes