Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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 dhh authored
2  activesupport/CHANGELOG
View
@@ -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]
10 activesupport/lib/active_support/test_case.rb
View
@@ -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

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.