Browse files

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

…HH via Jay Fields]
  • Loading branch information...
1 parent 048ac36 commit f74ba37f4e4175d5a1b31da59d161b0020b58e94 @dhh dhh committed Jun 12, 2008
Showing with 11 additions and 1 deletion.
  1. +2 −0 activesupport/CHANGELOG
  2. +9 −1 activesupport/lib/active_support/test_case.rb
View
2 activesupport/CHANGELOG
@@ -1,5 +1,7 @@
*Edge*
+* Added test/do declaration style testing to ActiveSupport::TestCase [DHH via Jay Fields]
+
* Added Object#present? which is equivalent to !Object#blank? [DHH]
* Added Enumberable#several? to encapsulate collection.size > 1 [DHH]
View
10 activesupport/lib/active_support/test_case.rb
@@ -9,5 +9,13 @@ class Test::Unit::TestCase #:nodoc:
module ActiveSupport
class TestCase < Test::Unit::TestCase
+ # test "verify something" do
+ # ...
+ # end
+ def self.test(name, &block)
+ test_name = "test_#{name.gsub(/[\s]/,'_')}".to_sym
+ raise "#{test_name} is already defined in #{self}" if self.instance_methods.include?(test_name.to_s)
+ define_method(test_name, &block)
+ end
end
-end
+end

4 comments on commit f74ba37

@seven1m

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)
@tsaleh

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?"
@

@tsaleh

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

@ymendel

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

Please sign in to comment.