<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: method_missing code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Wed, 23 Jul 2008 15:37:58 GMT</pubDate>
    <description>DZone Snippets: method_missing code</description>
    <item>
      <title>Rake's missing_method found!</title>
      <link>http://snippets.dzone.com/posts/show/2337</link>
      <description>From http://newbieonrails.topfunky.com/articles/2006/07/28/foscon-and-living-dangerously-with-rake:&lt;br /&gt;Updated from : http://mentalized.net/journal/2006/07/28/run_specific_tests_via_rake/&lt;br /&gt;&lt;br /&gt;If you define a rule with an empty string, you can catch any task that hasn&#8217;t been defined elsewhere. This makes it easy to dynamically create rake tasks. Essentially, this is method_missing for rake!&lt;br /&gt;&lt;code&gt;&lt;br /&gt;rule "" do |t|&lt;br /&gt;  t.name &lt;br /&gt;  # ... do something with the name of the task  &lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;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.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;##&lt;br /&gt;# Run a single test in Rails.&lt;br /&gt;#&lt;br /&gt;#   rake blogs_list&lt;br /&gt;#   =&gt; Runs test_list for BlogsController (functional test)&lt;br /&gt;#&lt;br /&gt;#   rake blog_create&lt;br /&gt;#   =&gt; Runs test_create for BlogTest (unit test)&lt;br /&gt;&lt;br /&gt;rule "" do |t|&lt;br /&gt;  if /(.*)_([^.]+)$/.match(t.name)&lt;br /&gt;    file_name = $1&lt;br /&gt;    test_name = $2&lt;br /&gt;    if File.exist?("test/unit/#{file_name}_test.rb")&lt;br /&gt;      file_name = "unit/#{file_name}_test.rb" &lt;br /&gt;    elsif File.exist?("test/functional/#{file_name}_controller_test.rb")&lt;br /&gt;      file_name = "functional/#{file_name}_controller_test.rb" &lt;br /&gt;    else&lt;br /&gt;      raise "No file found for #{file_name}" &lt;br /&gt;    end&lt;br /&gt;    sh "ruby -Ilib:test test/#{file_name} -n /^test_#{test_name}/" &lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Update from : http://mentalized.net/journal/2006/07/28/run_specific_tests_via_rake/&lt;br /&gt;This modified version uses a different syntax (rake test:foo:bar instead rake foo_bar) and regex matching for test names.&lt;br /&gt;&lt;br /&gt;Usage&lt;br /&gt;&lt;code&gt;&lt;br /&gt;$ rake test:blog&lt;br /&gt;=&gt; Runs the full BlogTest unit test&lt;br /&gt;&lt;br /&gt;$ rake test:blog:create&lt;br /&gt;=&gt; Runs the tests matching /create/ in the BlogTest unit test&lt;br /&gt;&lt;br /&gt;$ rake test:blog_controller&lt;br /&gt;=&gt; Runs all tests in the BlogControllerTest functional test&lt;br /&gt;&lt;br /&gt;$ rake test:blog_controller:create&lt;br /&gt;=&gt; Runs the tests matching /create/ in the BlogControllerTest functional test	&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Code&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# Run specific tests or test files&lt;br /&gt;# &lt;br /&gt;# rake test:blog&lt;br /&gt;# =&gt; Runs the full BlogTest unit test&lt;br /&gt;# &lt;br /&gt;# rake test:blog:create&lt;br /&gt;# =&gt; Runs the tests matching /create/ in the BlogTest unit test&lt;br /&gt;# &lt;br /&gt;# rake test:blog_controller&lt;br /&gt;# =&gt; Runs all tests in the BlogControllerTest functional test&lt;br /&gt;# &lt;br /&gt;# rake test:blog_controller&lt;br /&gt;# =&gt; Runs the tests matching /create/ in the BlogControllerTest functional test	&lt;br /&gt;rule "" do |t|&lt;br /&gt;  # test:file:method&lt;br /&gt;  if /test:(.*)(:([^.]+))?$/.match(t.name)&lt;br /&gt;    arguments = t.name.split(":")[1..-1]&lt;br /&gt;    file_name = arguments.first&lt;br /&gt;    test_name = arguments[1..-1] &lt;br /&gt;    &lt;br /&gt;    if File.exist?("test/unit/#{file_name}_test.rb")&lt;br /&gt;      run_file_name = "unit/#{file_name}_test.rb" &lt;br /&gt;    elsif File.exist?("test/functional/#{file_name}_test.rb")&lt;br /&gt;      run_file_name = "functional/#{file_name}_test.rb" &lt;br /&gt;    end&lt;br /&gt;    &lt;br /&gt;    sh "ruby -Ilib:test test/#{run_file_name} -n /#{test_name}/" &lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Do whatever you want with the above code, it&#8217;s yours now.&lt;br /&gt;</description>
      <pubDate>Fri, 28 Jul 2006 18:21:06 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2337</guid>
      <author>MattScilipoti (Matt Scilipoti)</author>
    </item>
  </channel>
</rss>
