Testing Rails 3.1 mountable engine with Rspec

三世轮回 提交于 2019-11-28 03:12:09

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.

I was looking for the same answer and I found the combustion gem* which promise to setup a full environment for spec'ing your engine in a simpler way. Just add

gem.add_development_dependency 'combustion', '~> 0.3.1'

to your gemspec and run

bundle exec combust

to reproduce a full rails app in your spec directory.

*I haven't tried it yet...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!