Can I run a Rails engine's specs from a real app which mounts it?

混江龙づ霸主 提交于 2019-12-03 03:51:25

The simplest solution would be to specify the paths in rspec command. If you have directory structure

/project
/engine
/engine_2

Then you do and should run all the specs

cd project
rspec spec/ ../engine/spec ../engine_2/spec

But if you want to run specs on Continous Integration or just this doesn't seem to be comfortable I solved this problem with a customized rake spec task, changing the pattern method.

lib/task/rspec.rake should look like this

require "rspec/core/rake_task"

RSpec::Core::RakeTask.new(:spec)

task :default => :spec
RSpec::Core::RakeTask.module_eval do
  def pattern
    extras = []
    Rails.application.config.rspec_paths.each do |dir|
      if File.directory?( dir )
        extras << File.join( dir, 'spec', '**', '*_spec.rb' ).to_s
      end
    end
    [@pattern] | extras
  end
end

In engine class you add a path to config.rspec_paths

class Engine < ::Rails::Engine
  # Register path to rspec
  config.rspec_paths << self.root
end

And don't forget to initialize config.rspec_paths somewhere in a base project.

If you want to add factories then you can create initializer, you can find solution somewhere here on stackoverflow.

Not sure if this solution is the best but works for me and I am happy with that. Good luck!

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