Skip to content

Instantly share code, notes, and snippets.

@lifo
Created September 30, 2008 21:21
Show Gist options
  • Save lifo/13962 to your computer and use it in GitHub Desktop.
Save lifo/13962 to your computer and use it in GitHub Desktop.
My new funky testing lib
require 'rubygems'
require 'active_support'
require 'active_support/test_case'
require 'test/unit'
class Test::Unit::TestCase
# Monkey it. T::U calls public_instance_methods(true)
def self.suite
method_names = public_instance_methods(false)
tests = method_names.delete_if {|method_name| method_name !~ /^test./}
suite = Test::Unit::TestSuite.new(name)
tests.sort.each do
|test|
catch(:invalid_test) do
suite << new(test)
end
end
if (suite.empty?)
catch(:invalid_test) do
suite << new("default_test")
end
end
suite
end
end
class ActiveSupport::TestCase
class << self
# COPY/PASTED FROM EDGE RAILS
def test(name, &block)
test_name = "test_#{name.gsub(/\s+/,'_')}".to_sym
defined = instance_method(test_name) rescue false
raise "#{test_name} is already defined in #{self}" if defined
if block_given?
define_method(test_name, &block)
else
define_method(test_name) do
flunk "No implementation provided for #{name}"
end
end
end
# (name) is just to fool you stupid
def context(name, &block)
Class.new(self, &block)
end
%w(contexts describe describes group specify specifies).each {|m| alias_method m, :context}
%w(it should tests).each {|m| alias_method m, :test}
end
end
class ExampleTest < ActiveSupport::TestCase
context "Hello" do
def setup
@hello = 1
end
def test_hello
assert_equal 1, @hello
assert_nil @world # FAILS
end
should "let you mix and match" do
assert true
end
describe "Hey I dont mean shit" do
setup :world
test "whatever the stuff" do
assert_equal 1, @world
assert_equal 1, @hello
end
it "should do something" do
assert true
end
private
def world
@world = 1
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment