module ActiveSupport::Testing::Deprecation
Публичные методы экземпляра
# File activesupport/lib/active_support/testing/deprecation.rb, line 31
def assert_deprecated(match = nil, deprecator = nil, &block)
result, warnings = collect_deprecations(deprecator, &block)
assert !warnings.empty?, "Expected a deprecation warning within the block but received none"
if match
match = Regexp.new(Regexp.escape(match)) unless match.is_a?(Regexp)
assert warnings.any? { |w| match.match?(w) }, "No deprecation warning matched #{match}: #{warnings.join(', ')}"
end
result
end Утверждает, что предупреждение о устаревании, соответствующее заданному значению, было выведено заданным deprecator во время выполнения переданного блока.
assert_deprecated(/foo/, CustomDeprecator) do CustomDeprecator.warn "foo should no longer be used" end
Объект match может быть Regexp, или String, появляющийся в сообщении.
assert_deprecated('foo', CustomDeprecator) do
CustomDeprecator.warn "foo should no longer be used"
end
Если match пропущен (или явно nil), любое предупреждение об устаревании будет соответствовать.
assert_deprecated(nil, CustomDeprecator) do CustomDeprecator.warn "foo should no longer be used" end
Если deprecator не задан, по умолчанию используется ActiveSupport::Deprecation.
assert_deprecated do ActiveSupport::Deprecation.warn "foo should no longer be used" end
# File activesupport/lib/active_support/testing/deprecation.rb, line 56
def assert_not_deprecated(deprecator = nil, &block)
result, deprecations = collect_deprecations(deprecator, &block)
assert deprecations.empty?, "Expected no deprecation warning within the block but received #{deprecations.size}: \n #{deprecations * "\n "}"
result
end Утверждает, что никаких предупреждений об устаревании не выводится заданным deprecator во время выполнения переданного блока.
assert_not_deprecated(CustomDeprecator) do CustomDeprecator.warn "message" # fails assertion end
Если deprecator не задан, по умолчанию используется ActiveSupport::Deprecation.
assert_not_deprecated do ActiveSupport::Deprecation.warn "message" # fails assertion end assert_not_deprecated do CustomDeprecator.warn "message" # passes assertion end
# File activesupport/lib/active_support/testing/deprecation.rb, line 75
def collect_deprecations(deprecator = nil)
deprecator ||= ActiveSupport::Deprecation
old_behavior = deprecator.behavior
deprecations = []
deprecator.behavior = Proc.new do |message, callstack|
deprecations << message
end
result = yield
[result, deprecations]
ensure
deprecator.behavior = old_behavior
end Возвращает массив всех предупреждений об устаревании, выведенных заданным deprecator во время выполнения переданного блока.
collect_deprecations(CustomDeprecator) do CustomDeprecator.warn "message" end # => ["message"]
Если deprecator не задан, по умолчанию используется ActiveSupport::Deprecation.
collect_deprecations do CustomDeprecator.warn "custom message" ActiveSupport::Deprecation.warn "message" end # => ["message"]
© 2004–2021 David Heinemeier Hansson
Licensed under the MIT License.