-
Notifications
You must be signed in to change notification settings - Fork 519
Expand file tree
/
Copy pathinvalid_transition_test.rb
More file actions
115 lines (90 loc) · 3.31 KB
/
invalid_transition_test.rb
File metadata and controls
115 lines (90 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
class InvalidTransitionTest < Test::Unit::TestCase
def setup
@klass = Class.new
@machine = StateMachine::Machine.new(@klass)
@state = @machine.state :parked
@machine.event :ignite
@object = @klass.new
@object.state = 'parked'
@invalid_transition = StateMachine::InvalidTransition.new(@object, @machine, :ignite)
end
def test_should_have_an_object
assert_equal @object, @invalid_transition.object
end
def test_should_have_a_machine
assert_equal @machine, @invalid_transition.machine
end
def test_should_have_an_event
assert_equal :ignite, @invalid_transition.event
end
def test_should_have_a_qualified_event
assert_equal :ignite, @invalid_transition.qualified_event
end
def test_should_have_a_from_value
assert_equal 'parked', @invalid_transition.from
end
def test_should_have_a_from_name
assert_equal :parked, @invalid_transition.from_name
end
def test_should_have_a_qualified_from_name
assert_equal :parked, @invalid_transition.qualified_from_name
end
def test_should_generate_a_message
assert_match 'Cannot transition state via :ignite from :parked', @invalid_transition.message
end
end
class InvalidTransitionWithNamespaceTest < Test::Unit::TestCase
def setup
@klass = Class.new
@machine = StateMachine::Machine.new(@klass, :namespace => 'alarm')
@state = @machine.state :active
@machine.event :disable
@object = @klass.new
@object.state = 'active'
@invalid_transition = StateMachine::InvalidTransition.new(@object, @machine, :disable)
end
def test_should_have_an_event
assert_equal :disable, @invalid_transition.event
end
def test_should_have_a_qualified_event
assert_equal :disable_alarm, @invalid_transition.qualified_event
end
def test_should_have_a_from_name
assert_equal :active, @invalid_transition.from_name
end
def test_should_have_a_qualified_from_name
assert_equal :alarm_active, @invalid_transition.qualified_from_name
end
end
class InvalidTransitionWithIntegrationTest < Test::Unit::TestCase
def setup
StateMachine::Integrations.const_set('Custom', Module.new do
include StateMachine::Integrations::Base
def errors_for(object)
object.errors
end
end)
@klass = Class.new do
attr_accessor :errors
end
@machine = StateMachine::Machine.new(@klass, :integration => :custom)
@machine.state :parked
@machine.event :ignite
@object = @klass.new
@object.state = 'parked'
end
def test_should_generate_a_message_without_reasons_if_empty
@object.errors = ''
invalid_transition = StateMachine::InvalidTransition.new(@object, @machine, :ignite)
assert_match 'Cannot transition state via :ignite from :parked', invalid_transition.message
end
def test_should_generate_a_message_with_error_reasons_if_errors_found
@object.errors = 'Id is invalid, Name is invalid'
invalid_transition = StateMachine::InvalidTransition.new(@object, @machine, :ignite)
assert_match 'Cannot transition state via :ignite from :parked (Reason(s): Id is invalid, Name is invalid)', invalid_transition.message
end
def teardown
StateMachine::Integrations.send(:remove_const, 'Custom')
end
end