DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Rake's Missing_method Found!

07.28.2006
| 4114 views |
  • submit to reddit
        From http://newbieonrails.topfunky.com/articles/2006/07/28/foscon-and-living-dangerously-with-rake:
Updated from : http://mentalized.net/journal/2006/07/28/run_specific_tests_via_rake/

If you define a rule with an empty string, you can catch any task that hasn’t been defined elsewhere. This makes it easy to dynamically create rake tasks. Essentially, this is method_missing for rake!
rule "" do |t|
  t.name 
  # ... do something with the name of the task  
end
I experimented by writing a rule that will take a task with the format of controllername_testname and automatically run a single test from the relevant functional or unit test.
##
# Run a single test in Rails.
#
#   rake blogs_list
#   => Runs test_list for BlogsController (functional test)
#
#   rake blog_create
#   => Runs test_create for BlogTest (unit test)

rule "" do |t|
  if /(.*)_([^.]+)$/.match(t.name)
    file_name = $1
    test_name = $2
    if File.exist?("test/unit/#{file_name}_test.rb")
      file_name = "unit/#{file_name}_test.rb" 
    elsif File.exist?("test/functional/#{file_name}_controller_test.rb")
      file_name = "functional/#{file_name}_controller_test.rb" 
    else
      raise "No file found for #{file_name}" 
    end
    sh "ruby -Ilib:test test/#{file_name} -n /^test_#{test_name}/" 
  end
end

Update from : http://mentalized.net/journal/2006/07/28/run_specific_tests_via_rake/
This modified version uses a different syntax (rake test:foo:bar instead rake foo_bar) and regex matching for test names.

Usage
$ rake test:blog
=> Runs the full BlogTest unit test

$ rake test:blog:create
=> Runs the tests matching /create/ in the BlogTest unit test

$ rake test:blog_controller
=> Runs all tests in the BlogControllerTest functional test

$ rake test:blog_controller:create
=> Runs the tests matching /create/ in the BlogControllerTest functional test	

Code
# Run specific tests or test files
# 
# rake test:blog
# => Runs the full BlogTest unit test
# 
# rake test:blog:create
# => Runs the tests matching /create/ in the BlogTest unit test
# 
# rake test:blog_controller
# => Runs all tests in the BlogControllerTest functional test
# 
# rake test:blog_controller
# => Runs the tests matching /create/ in the BlogControllerTest functional test	
rule "" do |t|
  # test:file:method
  if /test:(.*)(:([^.]+))?$/.match(t.name)
    arguments = t.name.split(":")[1..-1]
    file_name = arguments.first
    test_name = arguments[1..-1] 
    
    if File.exist?("test/unit/#{file_name}_test.rb")
      run_file_name = "unit/#{file_name}_test.rb" 
    elsif File.exist?("test/functional/#{file_name}_test.rb")
      run_file_name = "functional/#{file_name}_test.rb" 
    end
    
    sh "ruby -Ilib:test test/#{run_file_name} -n /#{test_name}/" 
  end
end
Do whatever you want with the above code, it’s yours now.
    

Comments

Snippets Manager replied on Thu, 2006/08/17 - 1:04am

you should mention that the user should create an arbitrary file ending in .rake and store this in the lib/tasks directory in order to enable this.