Skip to content
This repository
Browse code

Added test/do declaration style testing to ActiveSupport::TestCase [D…

…HH via Jay Fields]
  • Loading branch information...
commit f74ba37f4e4175d5a1b31da59d161b0020b58e94 1 parent 048ac36
David Heinemeier Hansson authored June 12, 2008
2  activesupport/CHANGELOG
... ...
@@ -1,5 +1,7 @@
1 1
 *Edge*
2 2
 
  3
+* Added test/do declaration style testing to ActiveSupport::TestCase [DHH via Jay Fields]
  4
+
3 5
 * Added Object#present? which is equivalent to !Object#blank? [DHH]
4 6
 
5 7
 * Added Enumberable#several? to encapsulate collection.size > 1 [DHH]
10  activesupport/lib/active_support/test_case.rb
@@ -9,5 +9,13 @@ class Test::Unit::TestCase #:nodoc:
9 9
 
10 10
 module ActiveSupport
11 11
   class TestCase < Test::Unit::TestCase
  12
+    # test "verify something" do                                   
  13
+    #   ...
  14
+    # end                                                     
  15
+    def self.test(name, &block)
  16
+      test_name = "test_#{name.gsub(/[\s]/,'_')}".to_sym
  17
+      raise "#{test_name} is already defined in #{self}" if self.instance_methods.include?(test_name.to_s)
  18
+      define_method(test_name, &block)
  19
+    end
12 20
   end
13  
-end
  21
+end

4 notes on commit f74ba37

Tim Morgan

Interesting. This is something I had done in my tests that looks somewhat similar. Only that mine can have “unimplemented” tests.


module TestExtensions
  def should(name, &block)
    if block_given?
      define_method 'test ' + name, &block
    else
      puts "Unimplemented: " + name
    end
  end
end

ActionController::TestCase.extend(TestExtensions)
Test::Unit::TestCase.extend(TestExtensions)
Tammer Saleh

Is there a reason for underscorizing the test names? doing the following is legal and more readable:

@ test_name = “test #{name}”.to_sym @

With the current implementation, a test like:

@ test “User’s should return [:a, :b] on #to_book?” @

(totally contrived) would produce a method like:

@ “test_User’s_should_return_[:a,:b]on#tobook?” @

instead of

@ test User’s should return [:a, :b] on #to_book?" @

Tammer Saleh

ok, no idea how to format code blocks in github comments :-/

Yossef Mendelssohn

Tammer: I learned that trick from you in shoulda, you nut.

Please sign in to comment.
Something went wrong with that request. Please try again.