2014-07-30 08:46:08 -04:00
|
|
|
gem 'minitest' # make sure we get the gem, not stdlib
|
|
|
|
require 'minitest'
|
2012-09-26 14:16:43 -04:00
|
|
|
require 'active_support/testing/tagged_logging'
|
2008-11-23 17:48:36 -05:00
|
|
|
require 'active_support/testing/setup_and_teardown'
|
|
|
|
require 'active_support/testing/assertions'
|
|
|
|
require 'active_support/testing/deprecation'
|
2012-12-28 12:06:10 -05:00
|
|
|
require 'active_support/testing/declarative'
|
2009-06-30 15:00:50 -04:00
|
|
|
require 'active_support/testing/isolation'
|
2012-09-24 15:44:49 -04:00
|
|
|
require 'active_support/testing/constant_lookup'
|
Add `#travel` and `#travel_to` to AS::TestCase
Add `ActiveSupport::Testing::TimeHelpers#travel` and `#travel_to`. These
methods change current time to the given time or time difference by
stubbing `Time.now` and `Date.today` to return the time or date after
the difference calculation, or the time or date that got passed into the
method respectively. These methods also accept a block, which will
return current time back to its original state at the end of the block.
Example for `#travel`:
Time.now # => 2013-11-09 15:34:49 -05:00
travel 1.day
Time.now # => 2013-11-10 15:34:49 -05:00
Date.today # => Sun, 10 Nov 2013
Example for `#travel_to`:
Time.now # => 2013-11-09 15:34:49 -05:00
travel_to Time.new(2004, 11, 24, 01, 04, 44)
Time.now # => 2004-11-24 01:04:44 -05:00
Date.today # => Wed, 24 Nov 2004
Both of these methods also accept a block, which will return the current
time back to its original state at the end of the block:
Time.now # => 2013-11-09 15:34:49 -05:00
travel 1.day do
User.create.created_at # => Sun, 10 Nov 2013 15:34:49 EST -05:00
end
travel_to Time.new(2004, 11, 24, 01, 04, 44) do
User.create.created_at # => Wed, 24 Nov 2004 01:04:44 EST -05:00
end
Time.now # => 2013-11-09 15:34:49 -05:00
This module is included in `ActiveSupport::TestCase` automatically.
2013-11-09 13:03:28 -05:00
|
|
|
require 'active_support/testing/time_helpers'
|
2015-01-23 09:45:34 -05:00
|
|
|
require 'active_support/testing/file_fixtures'
|
2010-03-17 16:44:24 -04:00
|
|
|
require 'active_support/core_ext/kernel/reporting'
|
2008-11-23 17:48:36 -05:00
|
|
|
|
2008-11-22 22:18:30 -05:00
|
|
|
module ActiveSupport
|
2013-05-06 20:38:45 -04:00
|
|
|
class TestCase < ::Minitest::Test
|
|
|
|
Assertion = Minitest::Assertion
|
|
|
|
|
2014-08-12 09:51:41 -04:00
|
|
|
class << self
|
2014-12-17 17:55:15 -05:00
|
|
|
# Sets the order in which test cases are run.
|
|
|
|
#
|
|
|
|
# ActiveSupport::TestCase.test_order = :random # => :random
|
|
|
|
#
|
|
|
|
# Valid values are:
|
|
|
|
# * +:random+ (to run tests in random order)
|
|
|
|
# * +:parallel+ (to run tests in parallel)
|
|
|
|
# * +:sorted+ (to run tests alphabetically by method name)
|
|
|
|
# * +:alpha+ (equivalent to +:sorted+)
|
2014-09-08 08:32:16 -04:00
|
|
|
def test_order=(new_order)
|
2014-09-10 23:54:43 -04:00
|
|
|
ActiveSupport.test_order = new_order
|
2014-09-08 08:32:16 -04:00
|
|
|
end
|
|
|
|
|
2014-12-17 17:55:15 -05:00
|
|
|
# Returns the order in which test cases are run.
|
|
|
|
#
|
2015-01-02 20:43:06 -05:00
|
|
|
# ActiveSupport::TestCase.test_order # => :random
|
2014-12-17 17:55:15 -05:00
|
|
|
#
|
|
|
|
# Possible values are +:random+, +:parallel+, +:alpha+, +:sorted+.
|
2015-01-02 20:43:06 -05:00
|
|
|
# Defaults to +:random+.
|
2014-09-08 08:32:16 -04:00
|
|
|
def test_order
|
2014-09-10 23:54:43 -04:00
|
|
|
test_order = ActiveSupport.test_order
|
|
|
|
|
|
|
|
if test_order.nil?
|
2015-01-02 20:43:06 -05:00
|
|
|
test_order = :random
|
2014-09-10 23:54:43 -04:00
|
|
|
self.test_order = test_order
|
2014-09-08 08:32:16 -04:00
|
|
|
end
|
|
|
|
|
2014-09-10 23:54:43 -04:00
|
|
|
test_order
|
2014-09-08 08:32:16 -04:00
|
|
|
end
|
|
|
|
|
2014-08-12 09:51:41 -04:00
|
|
|
alias :my_tests_are_order_dependent! :i_suck_and_my_tests_are_order_dependent!
|
|
|
|
end
|
|
|
|
|
2013-05-06 20:38:45 -04:00
|
|
|
alias_method :method_name, :name
|
2008-11-07 12:45:48 -05:00
|
|
|
|
2012-09-26 14:16:43 -04:00
|
|
|
include ActiveSupport::Testing::TaggedLogging
|
2008-11-07 12:45:48 -05:00
|
|
|
include ActiveSupport::Testing::SetupAndTeardown
|
|
|
|
include ActiveSupport::Testing::Assertions
|
2008-11-23 16:11:07 -05:00
|
|
|
include ActiveSupport::Testing::Deprecation
|
Add `#travel` and `#travel_to` to AS::TestCase
Add `ActiveSupport::Testing::TimeHelpers#travel` and `#travel_to`. These
methods change current time to the given time or time difference by
stubbing `Time.now` and `Date.today` to return the time or date after
the difference calculation, or the time or date that got passed into the
method respectively. These methods also accept a block, which will
return current time back to its original state at the end of the block.
Example for `#travel`:
Time.now # => 2013-11-09 15:34:49 -05:00
travel 1.day
Time.now # => 2013-11-10 15:34:49 -05:00
Date.today # => Sun, 10 Nov 2013
Example for `#travel_to`:
Time.now # => 2013-11-09 15:34:49 -05:00
travel_to Time.new(2004, 11, 24, 01, 04, 44)
Time.now # => 2004-11-24 01:04:44 -05:00
Date.today # => Wed, 24 Nov 2004
Both of these methods also accept a block, which will return the current
time back to its original state at the end of the block:
Time.now # => 2013-11-09 15:34:49 -05:00
travel 1.day do
User.create.created_at # => Sun, 10 Nov 2013 15:34:49 EST -05:00
end
travel_to Time.new(2004, 11, 24, 01, 04, 44) do
User.create.created_at # => Wed, 24 Nov 2004 01:04:44 EST -05:00
end
Time.now # => 2013-11-09 15:34:49 -05:00
This module is included in `ActiveSupport::TestCase` automatically.
2013-11-09 13:03:28 -05:00
|
|
|
include ActiveSupport::Testing::TimeHelpers
|
2015-01-23 09:45:34 -05:00
|
|
|
include ActiveSupport::Testing::FileFixtures
|
2012-12-28 12:06:10 -05:00
|
|
|
extend ActiveSupport::Testing::Declarative
|
2012-01-05 18:46:17 -05:00
|
|
|
|
|
|
|
# test/unit backwards compatibility methods
|
|
|
|
alias :assert_raise :assert_raises
|
2012-12-28 18:49:41 -05:00
|
|
|
alias :assert_not_empty :refute_empty
|
2012-01-05 18:46:17 -05:00
|
|
|
alias :assert_not_equal :refute_equal
|
2012-12-28 18:49:41 -05:00
|
|
|
alias :assert_not_in_delta :refute_in_delta
|
|
|
|
alias :assert_not_in_epsilon :refute_in_epsilon
|
|
|
|
alias :assert_not_includes :refute_includes
|
|
|
|
alias :assert_not_instance_of :refute_instance_of
|
|
|
|
alias :assert_not_kind_of :refute_kind_of
|
2012-01-05 18:46:17 -05:00
|
|
|
alias :assert_no_match :refute_match
|
2012-12-28 18:49:41 -05:00
|
|
|
alias :assert_not_nil :refute_nil
|
|
|
|
alias :assert_not_operator :refute_operator
|
|
|
|
alias :assert_not_predicate :refute_predicate
|
|
|
|
alias :assert_not_respond_to :refute_respond_to
|
2012-01-06 17:04:09 -05:00
|
|
|
alias :assert_not_same :refute_same
|
2012-01-05 18:46:17 -05:00
|
|
|
|
2012-12-05 01:11:54 -05:00
|
|
|
# Fails if the block raises an exception.
|
2012-06-19 16:08:13 -04:00
|
|
|
#
|
|
|
|
# assert_nothing_raised do
|
|
|
|
# ...
|
|
|
|
# end
|
2012-01-05 18:46:17 -05:00
|
|
|
def assert_nothing_raised(*args)
|
|
|
|
yield
|
|
|
|
end
|
2008-11-07 12:45:48 -05:00
|
|
|
end
|
2008-11-22 22:18:30 -05:00
|
|
|
end
|