Nepal on Rails

millisami's rants!

Setting Up Rails Engine With Rspec, Guard and Zeus

Update: One gain, you don’t have to cd into the spec/dummy folder. Just run from the same engine’s root files.

I love zeus for testing rails apps because of its speed. Recently I have re-started developing an engine and the default zeus start command is not enough for a rails engine.

So, found this gist and I could setup to run it. Here is the my copy of that gist. Do rename the my_engine with your engine name at line 8.

engine_plan.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
require 'zeus/rails'

ROOT_PATH = File.expand_path(Dir.pwd)
ENV_PATH  = File.expand_path('spec/dummy/config/environment',  ROOT_PATH)
BOOT_PATH = File.expand_path('spec/dummy/config/boot',  ROOT_PATH)
APP_PATH  = File.expand_path('spec/dummy/config/application',  ROOT_PATH)
ENGINE_ROOT = File.expand_path(Dir.pwd)
ENGINE_PATH = File.expand_path('lib/my_engine/engine', ENGINE_ROOT)

class EnginePlan < Zeus::Rails
end

Zeus.plan = EnginePlan.new

And now we need the zeus.json file as well.

zeus.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
{
  "command": "ruby -rubygems -r./engine_plan -eZeus.go",

  "plan": {
    "boot": {
      "default_bundle": {
        "development_environment": {
          "prerake": {"rake": []},
          "runner": ["r"],
          "console": ["c"],
          "server": ["s"],
          "generate": ["g"],
          "destroy": ["d"],
          "dbconsole": []
        },
        "test_environment": {
          "test_helper": {"test": ["rspec", "testrb"]}
        }
      }
    }
  }
}

With this setup, now lets run the specs. Install zeus with gem i zeus. In one window, start zeus with zeus start and in another window run the specs zeus rspec spec/file_spec.rb

If your spec pass, you might notice it will run it twice, upper one being green and another failing one. To mitigate this issue, in the spec_helper.rb file, just delete/comment the line require rspec/autorun.

Ahaa!! moment!! Wait, everytime I write/modify the code, I’ve to go to the terminal and run that command zeus rspec spec/file_spec.rb again and again.

You might be yelling, dude we have guard for this. Yeah!! Yeah!! Recently, guard-rspec has added the support for zeus, so lets install it first. Put it in the Gemfile and bundle it.

Gemfile
1
2
3
4
5
group :development do
  gem "guard"
  gem "guard-rspec", require: false
  gem "zeus", require: false
end

Then init the Guardfile

Guardfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
guard :rspec, cmd: 'zeus rspec', keep_failed: true, all_after_pass: true do
  watch(%r{^spec/.+_spec\.rb$})
  watch(%r{^lib/(.+)\.rb$})     { |m| "spec/lib/#{m[1]}_spec.rb" }
  watch('spec/spec_helper.rb')  { "spec" }

  # Rails example
  watch(%r{^app/(.+)\.rb$})                           { |m| "spec/#{m[1]}_spec.rb" }
  # watch(%r{^app/(.*)(\.erb|\.haml|\.slim)$})          { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
  watch(%r{^app/controllers/(.+)_(controller)\.rb$})  { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
  watch(%r{^spec/support/(.+)\.rb$})                  { "spec" }
  watch('config/routes.rb')                           { "spec/routing" }
  watch('app/controllers/application_controller.rb')  { "spec/controllers" }

#   # Capybara features specs
#   watch(%r{^app/views/(.+)/.*\.(erb|haml|slim)$})     { |m| "spec/features/#{m[1]}_spec.rb" }
end

Now you can run guard either with the bundle exec prefix or just the guard command if you have installed Guard outside the bundle with gem i guard

Happy Engine Testing!