Testing Rails 3.1 mountable engine with Rspec

前端 未结 2 1141
臣服心动
臣服心动 2020-12-07 12:12

I started making a Rails 3.1 engine, and I\'m having a hard time testing it using rspec.

First of all, if I run rails g integration_test whatever it cre

相关标签:
2条回答
  • 2020-12-07 12:53

    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...

    0 讨论(0)
  • 2020-12-07 13:01

    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.

    0 讨论(0)
提交回复
热议问题