2012-11-28 04:42:41 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
# all of these should be tested per case
|
|
|
|
# describe AASM, '- class level definitions' do
|
|
|
|
# it 'should define a class level methods on its including class' do
|
|
|
|
# Foo.should respond_to(:aasm_initial_state)
|
|
|
|
# Foo.should respond_to(:aasm_state)
|
|
|
|
# Foo.should respond_to(:aasm_event)
|
|
|
|
# Foo.should respond_to(:aasm_states)
|
|
|
|
# Foo.should respond_to(:aasm_states_for_select)
|
|
|
|
# Foo.should respond_to(:aasm_events)
|
|
|
|
# Foo.should respond_to(:aasm_from_states_for_state)
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
|
|
|
|
describe 'inspection for common cases' do
|
|
|
|
it 'should support the old DSL' do
|
|
|
|
Foo.aasm_states.should include(:open)
|
|
|
|
Foo.aasm_states.should include(:closed)
|
|
|
|
Foo.aasm_initial_state.should == :open
|
|
|
|
Foo.aasm_events.should include(:close)
|
|
|
|
Foo.aasm_events.should include(:null)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should support the new DSL' do
|
|
|
|
Foo.aasm.states.should include(:open)
|
|
|
|
Foo.aasm.states.should include(:closed)
|
2012-11-28 04:58:52 -05:00
|
|
|
Foo.aasm.initial_state.should == :open
|
2012-11-28 04:42:41 -05:00
|
|
|
Foo.aasm.events.should include(:close)
|
|
|
|
Foo.aasm.events.should include(:null)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should list states in the order they have been defined' do
|
|
|
|
Conversation.aasm.states.should == [:needs_attention, :read, :closed, :awaiting_response, :junk]
|
2008-04-14 15:57:34 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-11-28 04:42:41 -05:00
|
|
|
describe "special cases" do
|
|
|
|
it "should support valid a state name" do
|
2011-09-16 11:12:10 -04:00
|
|
|
Argument.aasm_states.should include(:invalid)
|
|
|
|
Argument.aasm_states.should include(:valid)
|
|
|
|
|
|
|
|
argument = Argument.new
|
|
|
|
argument.invalid?.should be_true
|
|
|
|
argument.aasm_current_state.should == :invalid
|
|
|
|
|
|
|
|
argument.valid!
|
|
|
|
argument.valid?.should be_true
|
|
|
|
argument.aasm_current_state.should == :valid
|
|
|
|
end
|
|
|
|
end
|
2008-04-14 15:57:34 -04:00
|
|
|
|
2012-11-28 04:42:41 -05:00
|
|
|
describe 'subclassing' do
|
2009-08-08 09:36:19 -04:00
|
|
|
it 'should have the parent states' do
|
|
|
|
Foo.aasm_states.each do |state|
|
|
|
|
FooTwo.aasm_states.should include(state)
|
2008-11-05 11:06:36 -05:00
|
|
|
end
|
2012-11-28 04:42:41 -05:00
|
|
|
Baz.aasm_states.should == Bar.aasm_states
|
2008-11-05 11:06:36 -05:00
|
|
|
end
|
2009-12-01 06:28:52 -05:00
|
|
|
|
2009-08-08 09:36:19 -04:00
|
|
|
it 'should not add the child states to the parent machine' do
|
|
|
|
Foo.aasm_states.should_not include(:foo)
|
2008-10-09 18:55:45 -04:00
|
|
|
end
|
|
|
|
|
2012-11-28 04:42:41 -05:00
|
|
|
it "should have the same events as its parent" do
|
|
|
|
Baz.aasm_events.should == Bar.aasm_events
|
|
|
|
end
|
|
|
|
end
|
2008-10-09 18:55:45 -04:00
|
|
|
|
2012-11-28 04:42:41 -05:00
|
|
|
describe :aasm_states_for_select do
|
2012-10-26 05:40:57 -04:00
|
|
|
it "should return a select friendly array of states" do
|
2008-05-29 10:05:50 -04:00
|
|
|
Foo.aasm_states_for_select.should == [['Open', 'open'], ['Closed', 'closed']]
|
2008-04-14 15:57:34 -04:00
|
|
|
end
|
2008-01-07 14:11:38 -05:00
|
|
|
end
|
|
|
|
|
2012-11-28 04:42:41 -05:00
|
|
|
describe :aasm_from_states_for_state do
|
2012-10-19 05:44:20 -04:00
|
|
|
it "should return all from states for a state" do
|
|
|
|
froms = AuthMachine.aasm_from_states_for_state(:active)
|
2012-10-19 06:05:43 -04:00
|
|
|
[:pending, :passive, :suspended].each {|from| froms.should include(from)}
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should return from states for a state for a particular transition only" do
|
|
|
|
froms = AuthMachine.aasm_from_states_for_state(:active, :transition => :unsuspend)
|
|
|
|
[:suspended].each {|from| froms.should include(from)}
|
2012-10-19 05:44:20 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-11-28 04:42:41 -05:00
|
|
|
describe 'instance methods' do
|
|
|
|
let(:foo) {Foo.new}
|
2008-01-07 14:11:38 -05:00
|
|
|
|
|
|
|
it 'should define a state querying instance method on including class' do
|
2012-11-28 04:42:41 -05:00
|
|
|
foo.should respond_to(:open?)
|
|
|
|
foo.should be_open
|
2008-01-07 14:11:38 -05:00
|
|
|
end
|
|
|
|
|
2012-11-28 04:42:41 -05:00
|
|
|
it 'should define an event! instance method' do
|
|
|
|
foo.should respond_to(:close!)
|
|
|
|
foo.close!
|
|
|
|
foo.should be_closed
|
2008-01-07 14:11:38 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe AASM, '- initial states' do
|
2012-11-28 04:42:41 -05:00
|
|
|
let(:foo) {Foo.new}
|
|
|
|
let(:bar) {Bar.new}
|
2008-01-07 14:11:38 -05:00
|
|
|
|
|
|
|
it 'should set the initial state' do
|
2012-11-28 04:42:41 -05:00
|
|
|
foo.aasm_current_state.should == :open
|
|
|
|
# foo.aasm.current_state.should == :open # not yet supported
|
|
|
|
foo.should be_open
|
|
|
|
foo.should_not be_closed
|
2008-01-07 14:11:38 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should use the first state defined if no initial state is given' do
|
2012-11-28 04:42:41 -05:00
|
|
|
bar.aasm_current_state.should == :read
|
|
|
|
# bar.aasm.current_state.should == :read # not yet supported
|
2008-01-07 14:11:38 -05:00
|
|
|
end
|
2009-05-27 20:32:02 -04:00
|
|
|
|
2009-02-26 12:06:43 -05:00
|
|
|
it 'should determine initial state from the Proc results' do
|
|
|
|
Banker.new(Banker::RICH - 1).aasm_current_state.should == :selling_bad_mortgages
|
|
|
|
Banker.new(Banker::RICH + 1).aasm_current_state.should == :retired
|
|
|
|
end
|
2008-01-07 14:11:38 -05:00
|
|
|
end
|
2008-02-21 10:16:08 -05:00
|
|
|
|
2012-11-28 04:42:41 -05:00
|
|
|
describe 'event firing without persistence' do
|
2008-04-29 01:27:56 -04:00
|
|
|
it 'should attempt to persist if aasm_write_state is defined' do
|
|
|
|
foo = Foo.new
|
2012-11-28 04:42:41 -05:00
|
|
|
def foo.aasm_write_state; end
|
2008-04-29 01:27:56 -04:00
|
|
|
|
2010-06-29 11:58:35 -04:00
|
|
|
foo.should_receive(:aasm_write_state_without_persistence).twice
|
2008-04-29 01:27:56 -04:00
|
|
|
foo.close
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-11-28 04:42:41 -05:00
|
|
|
describe :aasm_events_for_current_state do
|
|
|
|
let(:foo) {Foo.new}
|
2008-05-31 18:08:12 -04:00
|
|
|
|
2012-11-28 04:42:41 -05:00
|
|
|
it 'work' do
|
2012-11-28 04:56:52 -05:00
|
|
|
foo.aasm_events_for_current_state.should include(:close)
|
|
|
|
foo.aasm_events_for_current_state.should include(:null)
|
2008-05-31 18:08:12 -04:00
|
|
|
foo.close
|
2012-11-28 04:42:41 -05:00
|
|
|
foo.aasm_events_for_current_state.should be_empty
|
2008-06-01 17:48:17 -04:00
|
|
|
end
|
|
|
|
end
|
2008-06-04 00:44:25 -04:00
|
|
|
|
|
|
|
|