Rails: how to check code in lib / directory?

I have a model that gets its data from the parser object. I think the parser class should live in the lib / directory (although I can be convinced that it should live elsewhere). The question is: where should my module go for the parser class? And how can I guarantee that they will be executed every time I run a rake test?

+41
ruby-on-rails unit-testing
Apr 28 '09 at 16:51
source share
7 answers

In the Rails application I'm working on, I decided to just place the tests in the test\unit directory. I will also create them in the module / directory, 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 the last resistance, because these tests were carried out without any other changes.

+24
Apr 29 '09 at 1:00 a.m.
source share

Here is 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 

Replace the structure of your lib dir in the test directory, replacing the lib code with the appropriate tests.

Run rake test:lib to run the lib tests.

If you want all tests to run when rake test called, you can 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 } 
+20
Oct 19 '09 at 12:45
source share

I wanted to do the same, but with rspec and autospec, and it took a little digging to figure out where they get the list of directories / file templates that dictated the launch of the test files. I eventually 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 put my tests in the new spec / libs directory when the rpsec.rake file was set up for viewing in spec / lib. Just renaming libs -> lib did the trick!

+8
Jun 30 '10 at 21:02
source share

A simple and understandable way is to create a directory under the / unit / lib test. Then create a test like test / unit / lib / foo_test.rb corresponding to lib / foo.rb. No new rake tasks are required, and you can nest more directories if necessary to fit the lib directory structure.

+3
Feb 10 '14 at 10:32
source share

From 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 

Like Rails 4.1 , override test:run to enable 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 this example.

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

+3
Dec 20 '14 at 18:20
source share

In order not to define additional rake tasks for running tests from user defined folders, you can also run them using the rake test:all command. The structure of the test folders for the lib folder or any other custom folder is up to you. But I prefer to duplicate them in classes: lib corresponds to test/lib , app/form_objects to test/form_objects .

0
Jan 30 '14 at 19:16
source share

Using:

 [spring] rake test:all 

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

Omit [spring] if tou doesn't use it.

-2
03 Oct '14 at 1:59
source share



All Articles