Testing controllers in Rails engines with RSpec requires you to jump through some hoops. If memory serves, it was slightly trickier in Rails 3 than it is now in Rails 4. Fortunately the fix is pretty easy, if not obvious.

By the way, I'm standing on the shoulders of giants here. What I've done is to combine the most useful parts of two particular Stack Overflow answers, remove the now incorrect/irrelevant parts, and spoon-feed you the solution in the form of a complete example.

First we'll create our engine which we'll call "Appointly." (It's an imaginary appointment scheduling gem.)

$ rails plugin new appointly --mountable -T --dummy-path=spec/dummy

Next step is to add the rspec-rails gem to our gemspec:

# appointly.gemspec

s.add_dependency "rspec-rails", "~> 2.14.1"

And install RSpec:

$ bundle install
$ rails generate rspec:install

Everything has been straightforward so far. Now is where we have to modify spec/spec_helper.rb in exactly two places. I've tried to make it blatantly obvious what exactly it is you need to change.

# spec/spec_helper.rb

# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'

################################################################
# Change this
# require File.expand_path("../../config/environment", __FILE__)
#
# to this
require File.expand_path("../dummy/config/environment", __FILE__)
################################################################

require 'rspec/rails'
require 'rspec/autorun'

# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.

################################################################
# Change this
# Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
# To this
ENGINE_RAILS_ROOT=File.join(File.dirname(__FILE__), '../')
Dir[File.join(ENGINE_RAILS_ROOT, "spec/support/**/*.rb")].each {|f| require f }
################################################################

# Checks for pending migrations before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)

RSpec.configure do |config|
# ## Mock Framework
#
# If you prefer to use mocha, flexmock or RR, uncomment the 
# appropriate line:
#
# config.mock_with :mocha
# config.mock_with :flexmock
# config.mock_with :rr

# Remove this line if you're not using ActiveRecord or ActiveRecord
# fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"

# If you're not using ActiveRecord, or you'd prefer not to run each of 
# your examples within a transaction, remove the following line or
# assign false instead of true.
config.use_transactional_fixtures = true

# If true, the base class of anonymous controllers will be inferred
# automatically. This will be the default behavior in future versions of
# rspec-rails.
config.infer_base_class_for_anonymous_controllers = false

# Run specs in random order to surface order dependencies. If you find
# an order dependency and want to debug it, you can fix the order by 
# providing the seed, which is printed after each run.
# --seed 1234
config.order = "random"
end

In addition to changing those paths, we have to tell our engine to use RSpec as the default test framework:

# lib/appointly/engine.rb

module Appointly
class Engine < ::Rails::Engine
isolate_namespace Appointly

config.generators do |g|
g.test_framework :rspec
end
end
end

And now we're good to go. Let's generate a scaffold and create/migrate our database.

$ rails g scaffold appointment start_time:datetime
$ rm -rf test # For some reason Rails still generates the test directory
$ rake db:create
$ rake db:migrate RAILS_ENV=test

If we run one of our controller specs now...

$ rspec spec/controllers/appointly/appointments_controller_spec.rb:34

...it will fail. What happened? My understanding here is that since our engine is an isolated engine - an engine designed not to interfere with the routes, helpers, etc. defined in your application - we need to explicitly say we're using Appointly routes, not the dummy app's routes. Add use_route: :appointly to your spec:

describe "GET index" do
it "assigns all appointments as @appointments" do
appointment = Appointment.create! valid_attributes
# get :index, {}, valid_session
get :index, { use_route: :appointly }, valid_session
assigns(:appointments).should eq([appointment])
end
end

Run the spec again...

$ rspec spec/controllers/appointly/appointments_controller_spec.rb:34

...and it will pass.

Get the Honeybadger newsletter

Each month we share news, best practices, and stories from the DevOps & monitoring community—exclusively for developers like you.
    author photo
    Starr Horne

    Starr Horne is a Rubyist and Chief JavaScripter at Honeybadger.io. When she's not neck-deep in other people's bugs, she enjoys making furniture with traditional hand-tools, reading history and brewing beer in her garage in Seattle.

    More articles by Starr Horne
    An advertisement for Honeybadger that reads 'Turn your logs into events.'

    "Splunk-like querying without having to sell my kidneys? nice"

    That’s a direct quote from someone who just saw Honeybadger Insights. It’s a bit like Papertrail or DataDog—but with just the good parts and a reasonable price tag.

    Best of all, Insights logging is available on our free tier as part of a comprehensive monitoring suite including error tracking, uptime monitoring, status pages, and more.

    Start logging for FREE
    Simple 5-minute setup — No credit card required