Testing Rails 3.1 mountable engine with Rspec

I am using RSpec with a Rails engine without issues.

I created my plugin using the following switches: -T –full –dummy-path=spec/dummy.

  • -T excludes test/unit
  • --full indicates that the plugin is an engine
  • --dummy-path is simply so that we don’t get a test directory (the
    default is test/dummy).

From there I used the spec_helper from the “start your engines” article:

# Configure Rails Envinronment
ENV["RAILS_ENV"] = "test"
require File.expand_path("../dummy/config/environment.rb",  __FILE__)

require 'rspec/rails'

ENGINE_RAILS_ROOT=File.join(File.dirname(__FILE__), '../')

# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[File.join(ENGINE_RAILS_ROOT, "spec/support/**/*.rb")].each {|f| require f }

RSpec.configure do |config|
  config.use_transactional_fixtures = true
end

For the generators. I add a config.generators block to my engine.rb file like so:

module MyEngine
  class Engine < Rails::Engine
    config.generators do |g|
      g.test_framework :rspec, :view_specs => false
    end
  end
end

With that, I’m able to get rspec tests when running a generator like the model generator.

As for the DB, is your database.yml file set up correctly? Did you load the test environment, e.g. rake db:test:clone or rake db:migrate RAILS_ENV=test? My guess is that RSpec can’t see your tables because there isn’t a test database set up.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)