How do I share rspec examples across multiple applications?

I have a group of Rails applications that need to run the same rspec examples. These specifications are concerned with ensuring consistent agreements across an organization. I'm familiar with Rspec shared_example, but AFAIK, they can only be used in one application.

Is there a way to split rspec across multiple applications. If that helps, all applications use the Rails engine.

+3


source to share


2 answers


IMHO I don't think RSpec supports this. But this can be approached as the problem of exchanging codes between projects. Therefore, if you are already doing this in your organization, I am sticking to this decision to be consistent. Otherwise, you can try:

  • Creation of a gem (for example shared_specs

    ) with specifications. Require spec files in the gem lib/shared_spec.rb

    , then Gemfile

    add gem 'shared_specs', require: false

    and require 'shared_specs'

    in each application spec/shared_specs.rb

    . However, you want to publish the gem to your own gem server .
  • Use SCM ( git , svn , hg ) submodules to pull specs from a separate repository into the main directory spec

    . This requires familiarity with SCM, and the process can be cumbersome .
  • The solutions above reduce duplication, but create additional maintenance overhead - they require additional infrastructure, and updating in specifications means updating every application. Depending on the context (distributed commands at different rates, applications with similar but slightly different specifications, few applications, infrequent specification changes), a simple copy and paste of the specification file might also work.


(Each variation makes certain assumptions, but my SO points are too small to ask questions via comments.)

0


source


You need to use shared files with full_path (you can set this path with ENV variable):

require '/home/USERNAME/shared_examples/my_shared_specs.rb'

      

or

require ENV['SHARED_EXAMPLE_FILE_PATH']

      

Add this to your spec file and you can call it.

If you want to download more than one file, be sure to download all files in your rspec rails_helper.rb

( spec_helper.rb

if you have an older version), also make sure you download them before the files that use them ( documentation ).

rails_helper.rb



...
Dir['/home/USERNAME/shared_examples/*.rb'].each { |file| require file }
...

      

or

...
Dir["#{ ENV['SHARED_EXAMPLES_PATH'] }*.rb"].each { |file| require file }
...

      

Just make sure you replace the path I used with the one you have.

Another, more complicated gem would be creating a gem with only generic examples, and again yours rails_helper.rb

will need the files:

...
Dir["#{Bundler.load.specs.find{|s| s.name == <gem_name> }.full_gem_path}/shared_examples_path/*.rb"].each { |file| require file }
...

      

It could be a method in stone.

0


source







All Articles