55

I have a model which gets its data from a parser object. I'm thinking that the parser class should live in the lib/ directory (although I could be persuaded that it should live soewhere else). The question is: Where should my unit tests for the parser class be? And how do I ensure that they are run each time I run rake test?

7 Answers 7

30

In the Rails application I'm working on, I decided to just place the tests in the test\unit directory. I will also nest them by module/directory as well, for example:

lib/a.rb   => test/unit/a_test.rb
lib/b/c.rb => test/unit/b/c_test.rb

For me, this was the path of last resistance, as these tests ran without having to make any other changes.

2
  • 5
    @Qqwy since this does not work on Rails 5, what is the best practice for testing lib on Rails 5.2. Jul 18, 2018 at 16:15
  • Unfortunately, I do not know. :-(
    – Qqwy
    Jul 23, 2018 at 15:43
23

Here's one way:

Create lib/tasks/test_lib_dir.rake with the following

namespace :test do

  desc "Test lib source"
  Rake::TestTask.new(:lib) do |t|    
    t.libs << "test"
    t.pattern = 'test/lib/**/*_test.rb'
    t.verbose = true    
  end

end

Mimic the structure of your lib dir under the test dir, replacing lib code with corresponding tests.

Run rake test:lib to run your lib tests.

If you want all tests to run when you invoke rake test, you could add the following to your new rake file.

lib_task = Rake::Task["test:lib"]
test_task = Rake::Task[:test]
test_task.enhance { lib_task.invoke }
2
  • Somehow it doesn't load up the environment (specifically ActiveSupport's fixtures), and there's not way to force it in rakefile. May 18, 2012 at 14:45
  • 1
    Hey, I tried your solution and rake test:lib runs my test, but rake test does not. I copied it verbatim. Here's a link to my file contents: pastebin.com/FHMXU6vG Thanks in advance!
    – robert
    Aug 3, 2014 at 16:22
8

I was looking to do the same thing but with rspec & autospec and it took a little digging to figure out just where they were getting the list of directories / file patterns that dictated which test files to run. Ultimately I found this in lib/tasks/rspec.rake:86

  [:models, :controllers, :views, :helpers, :lib, :integration].each do |sub|
    desc "Run the code examples in spec/#{sub}"
    Spec::Rake::SpecTask.new(sub => spec_prereq) do |t|
      t.spec_opts = ['--options', "\"#{RAILS_ROOT}/spec/spec.opts\""]
      t.spec_files = FileList["spec/#{sub}/**/*_spec.rb"]
    end
  end

I had placed my tests in a new spec/libs directory when the rpsec.rake file was configured to look in spec/lib. Simply renaming libs -> lib did the trick!

1
  • I have /lib by default in my project somehow. Unfortunately rake test does't catch up the files, so this is RSpec-only and I have to use the solution above. May 18, 2012 at 13:47
3

An easy and clean way is just to create a directory under test/unit/lib. Then create test as test/unit/lib/foo_test.rb corresponding to lib/foo.rb. No new rake tasks required, and you can nest more directories if needed to match the lib directory structure.

3

As of Rails 4.0:

rake test:all         # Run all tests in any subdir of `test` without resetting the DB
rake test:all:db      # Same as above and resets the DB

As of Rails 4.1, redefine test:run to include additional tasks when running rake or rake test:

# lib/tasks/test.rake

namespace :test do
  Rake::Task["run"].clear
  task run: ["test:units", "test:functionals", "test:generators", "test:integration", "test:tasks"]

  ["tasks"].each do |name|
    Rails::TestTask.new(name => "test:prepare") do |t|
      t.pattern = "test/#{name}/**/*_test.rb"
    end
  end
end

This has the added bonus of defining rake test:tasks in the given example.

As of Rails 4.2, test:run includes all subdirs of test including them when running rake test, and thus rake.

1
  • 5
    In Rails 5, these different tasks no longer exist.
    – Qqwy
    Feb 11, 2017 at 3:07
0

To not define additional rake tasks to run tests from the custom defined folders you may also run them with the command rake test:all. Tests folders structure for the lib folder or any other custom folder is up to you. But I prefer to duplicate them in classes: lib is matched to test/lib, app/form_objects to test/form_objects.

-3

Use:

[spring] rake test:all

to run all tests, including the directories you created (like [root]/test/lib/).

Omit [spring] if tou aren't using it.

1
  • rake aborted! Don't know how to build task 'test:all'
    – user160917
    Nov 11, 2014 at 21:29

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.