Skip to content
Daniel Leong edited this page Oct 26, 2017 · 7 revisions

Getting zeus to work with SimpleCov is a bit of a pain. If you really want it though, this page tells you one way to get them to play better together.

SimpleCov hinges upon two things that do not work well with zeus:

  1. SimpleCov needs to start and exit within the same process.
  2. SimpleCov.start needs to start before you load your application code (files are tracked once they are required).

NOTE: This page discusses a solution for rspec, but test unit should work about the same. Minitest solution

Solution

To satisfy both of these, we need to put the code that starts SimpleCov in a method that Zeus calls instead of within the spec helper file. Next, we need to manually require every file that you want to ensure is tracked.

Here are the steps and code to get this working:

  1. If you do not already have a zeus.json file and custom_plan.rb, run zeus init
  2. Modify custom_plan.rb and spec_helper.rb as below
  3. Restart zeus
  4. Run your specs with zeus (zeus rspec spec)
  5. $$$ profit

custom_plan.rb:

require 'zeus/rails'

class CustomPlan < Zeus::Rails
  def test
    require 'simplecov'
    SimpleCov.start 
    # SimpleCov.start 'rails' if using RoR

    # require all ruby files
    Dir["#{Rails.root}/app/**/*.rb"].each { |f| load f }

    # run the tests
    super
  end
end
 
Zeus.plan = CustomPlan.new

spec_helper.rb:

# this part is optional, but it gets SimpleCov working when running
# specs without zeus (as long as zeus is not running)
def zeus_running?
  File.exists? '.zeus.sock'
end
 
if !zeus_running?
  require 'simplecov'
  SimpleCov.start
end

Resources

Minitest

Overwrite simplecov at_exit method and do result.format! with after_run.

SimpleCov.at_exit {}

Minitest.after_run do
   SimpleCov.result.format!
   ActiveRecord::Base.connection_pool.disconnect! if defined?(ActiveRecord)
end