Rails test fails to run

124 views Asked by At

I've encountered a strange issue with tests in Rails 6.1.3.1. I've created a file for generating various stats about my data, which is in lib/modules/stats.rb. I've added a test file in test/modules/stats_test.rb which runs this test, and I have 100% coverage.

These stats could be used in various places, one of which would be in a Rake task. So, I have created lib/tasks/stats.rake, along these lines:

require "#{Rails.root}/lib/modules/stats.rb"
include Stats

namespace :stats do

  desc 'Print all the stats'
  task :print => [:environment] do
    stats = StatsMethods.generate_all_the_stats
    puts stats
  end

end

By virtue of the first line of this file existing, rake test no longer runs test/modules/stats_test.rb; coverage drops proportionately. What's going on here?

1

There are 1 answers

0
knirirr On

Refactoring as follows resolved the issue, though I don't know why (suggestions welcome):

namespace :stats do

  desc 'Generate all the stats'
  task :generate => [:environment] do
    require File.join(Rails.root, 'lib', 'modules', 'stats.rb')
    include Stats
    stats = StatsMethods.generate_all_the_stats
    puts stats
  end

end