3

Edit 1

I created an issue for rspec-rails and made a repo so people can test it themselves.

Original post

I've build a plugin according to this stackoverflow answer and added a route in my engine as follows:

# /config/routes.rb

Myplugin::Engine.routes.draw do
  root to: 'pages#index'
end

My only rspec test is this:

# /spec/controllers/pages_controller_spec.rb

require "spec_helper"

describe Myplugin::PagesController do

  describe "GET /admin" do
    it "routes to pages#index" do
      expect(get: "/admin").to route_to(controller: "pages", action: "index")
    end
  end
end

And in my dummy app I have this route:

# /spec/dummy/config/routes.rb

Rails.application.routes.draw do
  mount Myplugin::Engine => "/admin"
end

I'm getting this error when running rspec in the root of the plugin:

Failures:

  1) Myplugin::PagesController GET /admin routes to pages#index
     Failure/Error: expect(get: "/admin").to route_to(controller: "pages", action: "index")
       No route matches "/admin"
     # ./spec/controllers/pages_controller_spec.rb:8:in `block (3 levels) in <top (required)>'

I've tried this stackoverflow answer, but it doesn't change the output.

Any ideas?

I'm using rspec 2.13.0 and rails 3.2.13.

Community
  • 1
  • 1
Flauwekeul
  • 869
  • 7
  • 24

1 Answers1

0

Dummy apps should not be used in you main application, but should be used to test plugins in isolation. For example if your plugin is named Myplugin and you wanted to test the plugin in isolation without any references to your main app, could you create a dummy app at vendors/plugins/my_plugin/spec/dummy and tests at vendors/plugins/admin/specor wherever you have you plugin stored. You use a dummy app because a plugin often can't run alone. Eg devise which is a good example on a plugin, needs a main app with a ApplicationController and a user model (plus some other stuff) to work.

If you only use your plugin in this application or you just want to test that it works correctly with your main app, then just create tests normally in spec and don't create a dummy app. This would work fine, as the plugin is loaded into your main app. You would of course need to have the following in your main route file:

# config/routes.rb
Rails.application.routes.draw do
  mount Myplugin::Engine => "/admin"
end

Feel free to ask if you have any questions about this.

jokklan
  • 3,520
  • 17
  • 37
  • Not sure if I follow. In this case I want to test if Myplugin makes the /admin route available to the host app (the app that will have Myplugin as a gem). Can't I use the dummy app (in Myplugin's spec/dummy folder) for this? In other words: can I use the app in myplugin/spec/dummy to test behavior as if it is a standalone app that has Myplugin as a gem? – Flauwekeul Jun 19 '13 at 12:17
  • Sorry maybe i misunderstood something in your question :s. But yes as long as you have your dummy app in the plugin folder, should it be fine... I though your where testing your plugin through another application that used that plugin. But if not, then everything you posted should work fine, and the problem must be somewhere else.. – jokklan Jun 19 '13 at 12:34