<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: TDD code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 17 May 2008 03:19:20 GMT</pubDate>
    <description>DZone Snippets: TDD code</description>
    <item>
      <title>.autotest file</title>
      <link>http://snippets.dzone.com/posts/show/3718</link>
      <description>&lt;code&gt;&lt;br /&gt;require 'autotest/redgreen'&lt;br /&gt;require 'autotest/timestamp'&lt;br /&gt;#require 'autotest/growl'&lt;br /&gt;&lt;br /&gt;#require 'autotest/html_report'&lt;br /&gt;#require 'autotest/kdenotify'&lt;br /&gt;#require 'autotest/menu'&lt;br /&gt;#require 'autotest/pretty'&lt;br /&gt;#require 'autotest/snarl'&lt;br /&gt;&lt;br /&gt;# Sort-of from http://blog.labratz.net/articles/2006/09/13/growl-autotest-rails-with-zentest-3-4-0&lt;br /&gt;def growl(title, msg, pri=0, stick="", image="")&lt;br /&gt;  image_arg = (!image.empty?) ? "--image #{image}" : ""&lt;br /&gt;  system "growlnotify -n autotest #{image_arg} -p #{pri} -m #{msg.inspect} #{title} #{stick}" &lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;Autotest.add_hook :red do |at|&lt;br /&gt;  growl("Tests Failed", "#{at.files_to_test.size} tests failed", 2, "", "/Applications/Mail.app/Contents/Resources/Caution.tiff")&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;Autotest.add_hook :green do |at|&lt;br /&gt;  growl("Tests Passed", "All tests passed", -2, "", "/Applications/Mail.app/Contents/Resources/certificate.tiff") #if at.tainted&lt;br /&gt;end  &lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 23 Mar 2007 10:31:52 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3718</guid>
      <author>caffo ()</author>
    </item>
    <item>
      <title>Tagging Your Test Cases</title>
      <link>http://snippets.dzone.com/posts/show/2632</link>
      <description>From http://www.railtie.net/articles/2006/09/17/tagging-your-test-cases:&lt;br /&gt;Here's a quick way to follow your tests a little more closely in your test.log. In your test setup, add the following line:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def setup&lt;br /&gt;  RAILS_DEFAULT_LOGGER.debug "\n\e[0;31mRUNNING TEST CASE: #{name}\e[m\n"&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 21 Sep 2006 08:59:06 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2632</guid>
      <author>MattScilipoti (Matt Scilipoti)</author>
    </item>
    <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>
    <item>
      <title>ClassLibrary Projects and App.config</title>
      <link>http://snippets.dzone.com/posts/show/2282</link>
      <description>This snippet comes from Jamie Cansdale at TestDriven.net (http://weblogs.asp.net/nunitaddin/archive/2006/06/07/ClassLibrary-Projects-and-App.config.aspx)&lt;br /&gt;&lt;br /&gt;In Visual Studio .NET 2003 if you want you unit tests to use an app config file you need to ensure a config file exists with the same path as your test assembly but ending with '.config'. Life has improved a bit with Visual Studio 2005 as it will automatically copy any 'App.config' file to the correct place even for ClassLibrary projects. You can achieve the same affect in VS2003 by using the following post-build event:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;copy "$(ProjectDir)App.config" "$(TargetPath).config"&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 17 Jul 2006 18:38:10 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2282</guid>
      <author>MattScilipoti (Matt Scilipoti)</author>
    </item>
    <item>
      <title>making functional test failures readable</title>
      <link>http://snippets.dzone.com/posts/show/1753</link>
      <description>making functional test failures readable&lt;br /&gt;&lt;br /&gt;By zenspider on Rails&lt;br /&gt;&lt;br /&gt;Tired of your rails functional test failures being completely unreadable? I'm not terribly fond of rails' assert_tag but it is better than nothing. However, I never like to read something like the following:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;expected tag, but no tag found matching {:attributes=&gt;{:action=&gt;"/admin/themes/update/1"}, :tag=&gt;"form"} in:&lt;br /&gt;"&lt;!DOCTYPE [...3k worth of crap cut...]&lt;/html&gt;".&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Ugh! It just does nothing to help you and since it isn't expressed as a diff, unit_diff is no help in this arena. I have however figured out how to make it much more manageable with the following snippets:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class ApplicationController &lt; ActionController::Base&lt;br /&gt; def initialize(testing=false)&lt;br /&gt; super()&lt;br /&gt; self.class.layout(nil) if testing&lt;br /&gt; end&lt;br /&gt; [...]&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;and then in your functional tests:&lt;br /&gt;&lt;br /&gt;def setup&lt;br /&gt; @controller = MyController.new(true)&lt;br /&gt; [...]&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Now your failures contain just the content from the page:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;expected tag, but no tag found matching {:attributes=&gt;{:action=&gt;"/admin/themes/update/1"}, :tag=&gt;"form"} in:&lt;br /&gt;"\n&lt;ul&gt;\n &lt;li&gt;Name: Blue&lt;/li&gt;\n &lt;li&gt;Folder: blue&lt;/li&gt;\n &lt;li&gt;Description: A Plain blue theme&lt;/li&gt;\n &lt;li&gt;Masthead: Blue&lt;/li&gt;\n &lt;li&gt;Editor: n/a&lt;/li&gt;\n&lt;/ul&gt;\n\n&lt;a href=\"/admin/themes/show/1\"&gt;Cancel Editing&lt;/a&gt;\n".&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 24 Mar 2006 09:03:01 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1753</guid>
      <author>MattScilipoti (Matt Scilipoti)</author>
    </item>
    <item>
      <title>Partial Mock Object or mocking an instance of an object.</title>
      <link>http://snippets.dzone.com/posts/show/1687</link>
      <description>From: http://www.karmiccoding.com/articles/2006/03/11/under-the-hood-with-ruby-partial-mock-objects-for-unit-testing&lt;br /&gt;&lt;br /&gt;"What happens though, if you want to override a class in an instance of an object, and not all of its kind? Typically you would define a mock object, and create an instance of it. But, in Ruby there is an easier and faster way that doesn&#8217;t involve writing a different mock class for each different scenario &#8211; and it is made possible by the singleton class. This clever bit of ruby hackery lets you override the behaviour of a single instance of a class, creating what I&#8217;ve decided to call a partial mock object. To demonstrate, I&#8217;ve written a small method called override_method which will override the behaviour of the specified method in the passed object, like so:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# Overrides the method +method_name+ in +obj+ with the passed block&lt;br /&gt;def override_method(obj, method_name, &amp;block)&lt;br /&gt;  # Get the singleton class/eigenclass for 'obj'&lt;br /&gt;  klass = class &lt;&lt;obj; self; end &lt;br /&gt;&lt;br /&gt;  # Undefine the old method (using 'send' since 'undef_method' is protected)&lt;br /&gt;  klass.send(:undef_method, method_name)&lt;br /&gt;&lt;br /&gt;  # Create the new method&lt;br /&gt;  klass.send(:define_method, method_name, block)&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;# Just an example class&lt;br /&gt;class Foo &lt;br /&gt;  def do_stuff&lt;br /&gt;    "I'm okay!" &lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;# Test code&lt;br /&gt;list = []&lt;br /&gt;5.times { list.push(Foo.new) }&lt;br /&gt;&lt;br /&gt;# We override the method here!&lt;br /&gt;override_method(list.first, :do_stuff) { "I'm NOT okay!" }&lt;br /&gt;&lt;br /&gt;list.each_with_index { |f, i| puts "(#{i}) #{f.do_stuff}" }&lt;br /&gt;&lt;br /&gt;Outputs:&lt;br /&gt;(0) I'm NOT okay!&lt;br /&gt;(1) I'm okay!&lt;br /&gt;(2) I'm okay!&lt;br /&gt;(3) I'm okay!&lt;br /&gt;(4) I'm okay!&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;As you can see, only the first object in the array&#8217;s behaviour has been changed &#8211; the rest have remained untouched. Because of this, you can embed these partial dynamic mock objects deeply into your code without the need to specially instantiate a mock object deep in your code, or writing a &#8216;clever mock&#8217; to only trigger the determined behaviour in certain objects.&lt;br /&gt;&lt;br /&gt;Where this code comes in really handy is when you need an object to raise a difficult to simulate exception (like a disk full error) on a certain method to test your error handling &#8211; simply call override_method and pass in a call to raise and voila! Dynamic partial mock objects on the fly!"&lt;br /&gt;</description>
      <pubDate>Sun, 12 Mar 2006 09:33:38 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1687</guid>
      <author>MattScilipoti (Matt Scilipoti)</author>
    </item>
  </channel>
</rss>
