Never been to DZone Snippets before?

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

About this user

Matt Scilipoti

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Rake's missing_method found!

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!
   1  
   2  rule "" do |t|
   3    t.name 
   4    # ... do something with the name of the task  
   5  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.
   1  
   2  ##
   3  # Run a single test in Rails.
   4  #
   5  #   rake blogs_list
   6  #   => Runs test_list for BlogsController (functional test)
   7  #
   8  #   rake blog_create
   9  #   => Runs test_create for BlogTest (unit test)
  10  
  11  rule "" do |t|
  12    if /(.*)_([^.]+)$/.match(t.name)
  13      file_name = $1
  14      test_name = $2
  15      if File.exist?("test/unit/#{file_name}_test.rb")
  16        file_name = "unit/#{file_name}_test.rb" 
  17      elsif File.exist?("test/functional/#{file_name}_controller_test.rb")
  18        file_name = "functional/#{file_name}_controller_test.rb" 
  19      else
  20        raise "No file found for #{file_name}" 
  21      end
  22      sh "ruby -Ilib:test test/#{file_name} -n /^test_#{test_name}/" 
  23    end
  24  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
   1  
   2  $ rake test:blog
   3  => Runs the full BlogTest unit test
   4  
   5  $ rake test:blog:create
   6  => Runs the tests matching /create/ in the BlogTest unit test
   7  
   8  $ rake test:blog_controller
   9  => Runs all tests in the BlogControllerTest functional test
  10  
  11  $ rake test:blog_controller:create
  12  => Runs the tests matching /create/ in the BlogControllerTest functional test	


Code
   1  
   2  # Run specific tests or test files
   3  # 
   4  # rake test:blog
   5  # => Runs the full BlogTest unit test
   6  # 
   7  # rake test:blog:create
   8  # => Runs the tests matching /create/ in the BlogTest unit test
   9  # 
  10  # rake test:blog_controller
  11  # => Runs all tests in the BlogControllerTest functional test
  12  # 
  13  # rake test:blog_controller
  14  # => Runs the tests matching /create/ in the BlogControllerTest functional test	
  15  rule "" do |t|
  16    # test:file:method
  17    if /test:(.*)(:([^.]+))?$/.match(t.name)
  18      arguments = t.name.split(":")[1..-1]
  19      file_name = arguments.first
  20      test_name = arguments[1..-1] 
  21      
  22      if File.exist?("test/unit/#{file_name}_test.rb")
  23        run_file_name = "unit/#{file_name}_test.rb" 
  24      elsif File.exist?("test/functional/#{file_name}_test.rb")
  25        run_file_name = "functional/#{file_name}_test.rb" 
  26      end
  27      
  28      sh "ruby -Ilib:test test/#{run_file_name} -n /#{test_name}/" 
  29    end
  30  end

Do whatever you want with the above code, it’s yours now.
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS