2018-12-25 09:35:15 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-09-29 20:50:43 -04:00
|
|
|
require_relative "abstract_unit"
|
2018-12-25 09:35:15 -05:00
|
|
|
require "active_support/actionable_error"
|
|
|
|
|
|
|
|
class ActionableErrorTest < ActiveSupport::TestCase
|
2018-12-29 10:27:33 -05:00
|
|
|
NonActionableError = Class.new(StandardError)
|
2018-12-25 09:35:15 -05:00
|
|
|
|
|
|
|
class DispatchableError < StandardError
|
|
|
|
include ActiveSupport::ActionableError
|
|
|
|
|
|
|
|
class_attribute :flip1, default: false
|
|
|
|
class_attribute :flip2, default: false
|
|
|
|
|
|
|
|
action "Flip 1" do
|
|
|
|
self.flip1 = true
|
|
|
|
end
|
|
|
|
|
|
|
|
action "Flip 2" do
|
|
|
|
self.flip2 = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-01 05:07:20 -05:00
|
|
|
test "returns all action of an actionable error" do
|
2018-12-25 09:35:15 -05:00
|
|
|
assert_equal ["Flip 1", "Flip 2"], ActiveSupport::ActionableError.actions(DispatchableError).keys
|
|
|
|
assert_equal ["Flip 1", "Flip 2"], ActiveSupport::ActionableError.actions(DispatchableError.new).keys
|
|
|
|
end
|
|
|
|
|
2019-01-01 05:07:20 -05:00
|
|
|
test "returns no actions for non-actionable errors" do
|
|
|
|
assert ActiveSupport::ActionableError.actions(Exception).empty?
|
2018-12-29 10:27:33 -05:00
|
|
|
assert ActiveSupport::ActionableError.actions(Exception.new).empty?
|
2018-12-25 09:35:15 -05:00
|
|
|
end
|
|
|
|
|
2019-01-01 05:07:20 -05:00
|
|
|
test "dispatches actions from error and name" do
|
2018-12-25 09:35:15 -05:00
|
|
|
assert_changes "DispatchableError.flip1", from: false, to: true do
|
|
|
|
ActiveSupport::ActionableError.dispatch DispatchableError, "Flip 1"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-01 05:07:20 -05:00
|
|
|
test "cannot dispatch missing actions" do
|
2018-12-25 09:35:15 -05:00
|
|
|
err = assert_raises ActiveSupport::ActionableError::NonActionable do
|
|
|
|
ActiveSupport::ActionableError.dispatch NonActionableError, "action"
|
|
|
|
end
|
|
|
|
|
2019-01-01 05:07:20 -05:00
|
|
|
assert_equal 'Cannot find action "action"', err.to_s
|
2018-12-25 09:35:15 -05:00
|
|
|
end
|
|
|
|
end
|