ruby-on-railsrubyrakerake-test

Rails extending `rake test` to incorporate custom behaviour


I've got a bunch of files in test/policies, and I've tried to enhance rake test like so:

# lib/tasks/test.rake
namespace :test do
  desc "Test Pundit policies"
  Rake::TestTask.new(:policies) do |t|
    t.libs << 'test'
    t.pattern = 'test/policies/*_test.rb'
  end 
end

Rake::Task["test"].enhance do
  Rake::Task["test:policies"].invoke
end

It works great if I run bin/rake test:all, but bin/rake test now only runs the policy tests, and none of my others.

Can anyone advise what I am doing wrong here? In case it's not clear, I want rake test to run all my tests, just like it used to.

UPDATE

Actually, it's kind of working now, but I've noticed that my functional, unit and integration tests do run now, but only if the policy tests all pass. If any of the policy tests fail, then the rest of my test suite fails to run.

And I don't like the output, notice how there are two blocks of test output below:

[vagrant@vagrant-centos-6-4 vagrant]$ bin/rake test
Run options: --seed 54880

# Running:

.........................

Finished in 0.584530s, 42.7694 runs/s, 56.4556 assertions/s.


25 runs, 33 assertions, 0 failures, 0 errors, 0 skips
Run options: --seed 19900

# Running:

.........................................................

Finished in 4.132299s, 51.0612 runs/s, 128.9839 assertions/s.

I'd prefer if the policy tests output was merged into the same output block as that from my other tests. Any ideas, or is this as good as it's going to get?


Solution

  • Someone else asked the same question and I answered it for them. The solution in my case was to change my code from:

    Rake::TestTask.new(:policies) do |t|
    

    to:

    Rails::TestTask.new(:policies) do |t|
    

    It just works, and fixes all the issues I was having.