Permalink
Browse files

Add better invalid transition message.

  • Loading branch information...
troessner committed Apr 16, 2012
1 parent cab16be commit bfd8938fd7b7f29f51eb1bd95a2bfa3869a4e177
Showing with 11 additions and 3 deletions.
  1. +5 −1 lib/transitions/event.rb
  2. +6 −2 test/test_event_being_fired.rb
View
@@ -40,7 +40,7 @@ def initialize(machine, name, options = {}, &block)
def fire(obj, to_state = nil, *args)
transitions = @transitions.select { |t| t.from == obj.current_state(@machine ? @machine.name : nil) }
- raise InvalidTransition if transitions.size == 0
+ raise InvalidTransition, error_message_for_invalid_transitions(obj) if transitions.size == 0
next_state = nil
transitions.each do |transition|
@@ -133,5 +133,9 @@ def build_sucess_callback(callback_symbol_or_proc)
lambda { |record| record.send(callback_symbol_or_proc) }
end
end
+
+ def error_message_for_invalid_transitions(obj)
+ "No transitions present for `#{obj.class.name}` #{obj.class < ActiveRecord::Base && obj.persisted? ? "with ID #{obj.id} " : nil}with current state `#{obj.current_state}`"
+ end
end
end
@@ -3,10 +3,14 @@
class TestEventBeingFired < Test::Unit::TestCase
test "should raise an Transitions::InvalidTransition error if the transitions are empty" do
event = Transitions::Event.new(nil, :event)
+ class AnotherDummy; end
+ obj = AnotherDummy.new
+ obj.stubs(:current_state).returns(:running)
- assert_raise Transitions::InvalidTransition do
- event.fire(nil)
+ exception = assert_raise Transitions::InvalidTransition do
+ event.fire(obj)
end
+ assert_match /No transitions present for `TestEventBeingFired::AnotherDummy` with current state `running`/, exception.message
end
test "should return the state of the first matching transition it finds" do

0 comments on commit bfd8938

Please sign in to comment.