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

后端 未结 1 1879
春和景丽
春和景丽 2021-02-06 04:02

I have a Rails Engine meant to provide some models and controllers to a larger project of ours. There\'s a pretty decent set of specs for the Engine, using a bunch of mocks and

1条回答
  •  旧巷少年郎
    2021-02-06 04:39

    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!

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