Skip to content

Commit

Permalink
Added test/do declaration style testing to ActiveSupport::TestCase [D…
Browse files Browse the repository at this point in the history
…HH via Jay Fields]
  • Loading branch information
dhh committed Jun 13, 2008
1 parent 048ac36 commit f74ba37
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 2 additions & 0 deletions 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]
Expand Down
10 changes: 9 additions & 1 deletion activesupport/lib/active_support/test_case.rb
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

@tammersaleh
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@tammersaleh
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@ymendel
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Please sign in to comment.