Search code examples
ruby-on-railsruby-on-rails-4rspecautomated-testsrails-engines

How to tell RSpec to restart Rails before an example is run?


Is it possible to tell rspec to restart Rails before an example is run? I'm building an Engine that hooks into the Rails initialization process and the users can make some configuration changes, in an initializer, that impact how Rails and the Engine are configured. I want to be able to simulate those configuration changes, restart rails and test the result.


Solution

  • I haven't done this feat yet, but as best practice I think your engine tests should be part of the engine and should have minimal dependencies.

    Some approaches I've seen and believe you should try and combine:

    1. Mock a minimal parent rails app to test your engine.
    2. Write multiple dummy apps to test with.
    3. Instead of loading the entire rails application, you can split spec_helper and rails_helper in smaller parts, also gaining in setup time.
    4. You can write custom rake tasks to switch environment before spawning a new test thread.
    5. You can also overwrite at runtime the configuration values which reflect in your test (plus: use dependency injection!).
    6. If your initializer is complex enough, you could extract it in a testable helper and wire it up in your test initializers.
    7. Also, there seems to be a gem for that: Combustion.