Question

I have an admin engine with rspec inside my host application. Here is the my application structure:

Structure of my application

Here is the my admin engine's config:

module Admin
  class Engine < ::Rails::Engine
    isolate_namespace Admin
    engine_name 'admin'
    config.generators do |g|
      g.test_framework :rspec, fixture: false, view_specs: false
      g.fixture_replacement :fabrication
      g.fixture_replacement :factory_girl, dir: 'spec/factories'
      g.integration_tool :rspec
      g.assets false
      g.helper false
    end
  end
end

When I create new controller inside the admin engine. I want to generate controller tests on host application's spec/admin/controllers/welcome_controller_spec.rb. I guess I need change admin engine's rspec's config. Any idea?

Was it helpful?

Solution

In your admin project, in config/initializers directory you can create a monkey patch to overwrite the path where the controller spec file is generated:

file /config/initializers/scaffold_generator.rb

require 'generators/rspec/scaffold/scaffold_generator'

module Rspec
  module Generators
    class ScaffoldGenerator

      def generate_controller_spec
        return unless options[:controller_specs]

        template 'controller_spec.rb',
                 File.join('/path/to/host/project', 'spec/admin/controllers', controller_class_path, "#{controller_file_name}_controller_spec.rb")
      end

    end
  end
end

It would be clever to replace the hard-coded path '/path/to/host/project' by something more dynamic, so it won't break when you move your workspace files in another location. I can't help you for that because it depends on your project file structure, and it should be easy to do.

To invoke:

bundle exec rails generate scaffold_controller my_controller

Will generate the controller, helper, views, rspec/helper, and rspec/views files in your admin project, and the rspec/controller file in your host project

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top