mirror of
https://github.com/aasm/aasm
synced 2023-03-27 23:22:41 -04:00
improving tests
This commit is contained in:
parent
25ad26332b
commit
d8b8d5f5c3
6 changed files with 55 additions and 88 deletions
|
@ -21,6 +21,7 @@ module AASM
|
|||
@aasm
|
||||
end
|
||||
|
||||
# TODO: maybe better: aasm.initial_state
|
||||
def aasm_initial_state(set_state=nil)
|
||||
if set_state
|
||||
# deprecated way to set the value
|
||||
|
@ -68,7 +69,8 @@ module AASM
|
|||
aasm.states_for_select
|
||||
end
|
||||
|
||||
def aasm_human_event_name(event)
|
||||
# aasm.event(:event_name).human?
|
||||
def aasm_human_event_name(event) # event_name?
|
||||
AASM::SupportingClasses::Localizer.new.human_event_name(self, event)
|
||||
end
|
||||
end # ClassMethods
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
module AASM
|
||||
class Base
|
||||
|
||||
def initialize(clazz, options={}, &block)
|
||||
@clazz = clazz
|
||||
sm = AASM::StateMachine[@clazz]
|
||||
|
|
|
@ -6,10 +6,12 @@ class CallbackNewDsl
|
|||
:before_enter => :before_enter_open,
|
||||
:after_enter => :after_enter_open,
|
||||
:before_exit => :before_exit_open,
|
||||
:exit => :exit_open,
|
||||
:after_exit => :after_exit_open
|
||||
|
||||
state :closed,
|
||||
:before_enter => :before_enter_closed,
|
||||
:enter => :enter_closed,
|
||||
:after_enter => :after_enter_closed,
|
||||
:before_exit => :before_exit_closed,
|
||||
:after_exit => :after_exit_closed
|
||||
|
@ -35,4 +37,7 @@ class CallbackNewDsl
|
|||
|
||||
def before; end
|
||||
def after; end
|
||||
|
||||
def enter_closed; end
|
||||
def exit_open; end
|
||||
end
|
||||
|
|
|
@ -4,14 +4,16 @@ class CallbackOldDsl
|
|||
aasm_initial_state :open
|
||||
aasm_state :open,
|
||||
:before_enter => :before_enter_open,
|
||||
:before_exit => :before_exit_open,
|
||||
:after_enter => :after_enter_open,
|
||||
:after_exit => :after_exit_open
|
||||
:after_enter => :after_enter_open,
|
||||
:before_exit => :before_exit_open,
|
||||
:exit => :exit_open,
|
||||
:after_exit => :after_exit_open
|
||||
aasm_state :closed,
|
||||
:before_enter => :before_enter_closed,
|
||||
:before_exit => :before_exit_closed,
|
||||
:after_enter => :after_enter_closed,
|
||||
:after_exit => :after_exit_closed
|
||||
:enter => :enter_closed,
|
||||
:after_enter => :after_enter_closed,
|
||||
:before_exit => :before_exit_closed,
|
||||
:after_exit => :after_exit_closed
|
||||
|
||||
aasm_event :close, :before => :before, :after => :after do
|
||||
transitions :to => :closed, :from => [:open]
|
||||
|
@ -33,4 +35,7 @@ class CallbackOldDsl
|
|||
|
||||
def before; end
|
||||
def after; end
|
||||
|
||||
def enter_closed; end
|
||||
def exit_open; end
|
||||
end
|
||||
|
|
|
@ -1,49 +0,0 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe 'callbacks for the new DSL' do
|
||||
before(:each) do
|
||||
@callback = CallbackNewDsl.new
|
||||
end
|
||||
|
||||
it "should get close callbacks" do
|
||||
@callback.should_receive(:before).once.ordered
|
||||
@callback.should_receive(:before_exit_open).once.ordered # these should be before the state changes
|
||||
@callback.should_receive(:before_enter_closed).once.ordered
|
||||
@callback.should_receive(:aasm_write_state).once.ordered.and_return(true) # this is when the state changes
|
||||
@callback.should_receive(:after_exit_open).once.ordered # these should be after the state changes
|
||||
@callback.should_receive(:after_enter_closed).once.ordered
|
||||
@callback.should_receive(:after).once.ordered
|
||||
|
||||
@callback.close!
|
||||
end
|
||||
|
||||
it "should get open callbacks" do
|
||||
@callback.close!
|
||||
|
||||
@callback.should_receive(:before).once.ordered
|
||||
@callback.should_receive(:before_exit_closed).once.ordered # these should be before the state changes
|
||||
@callback.should_receive(:before_enter_open).once.ordered
|
||||
@callback.should_receive(:aasm_write_state).once.ordered.and_return(true) # this is when the state changes
|
||||
@callback.should_receive(:after_exit_closed).once.ordered # these should be after the state changes
|
||||
@callback.should_receive(:after_enter_open).once.ordered
|
||||
@callback.should_receive(:after).once.ordered
|
||||
|
||||
@callback.open!
|
||||
end
|
||||
end
|
||||
|
||||
describe AASM, '- state actions' do
|
||||
it "should call enter when entering state" do
|
||||
foo = Foo.new
|
||||
foo.should_receive(:enter)
|
||||
|
||||
foo.close
|
||||
end
|
||||
|
||||
it "should call exit when exiting state" do
|
||||
foo = Foo.new
|
||||
foo.should_receive(:exit)
|
||||
|
||||
foo.close
|
||||
end
|
||||
end
|
|
@ -1,38 +1,42 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe 'callbacks for the old DSL' do
|
||||
before(:each) do
|
||||
@callback = CallbackOldDsl.new
|
||||
end
|
||||
let(:callback) {CallbackOldDsl.new}
|
||||
|
||||
it "should get close callbacks" do
|
||||
@callback.should_receive(:before).once.ordered
|
||||
@callback.should_receive(:before_exit_open).once.ordered # these should be before the state changes
|
||||
@callback.should_receive(:before_enter_closed).once.ordered
|
||||
@callback.should_receive(:aasm_write_state).once.ordered.and_return(true) # this is when the state changes
|
||||
@callback.should_receive(:after_exit_open).once.ordered # these should be after the state changes
|
||||
@callback.should_receive(:after_enter_closed).once.ordered
|
||||
@callback.should_receive(:after).once.ordered
|
||||
callback.should_receive(:exit_open).once.ordered
|
||||
callback.should_receive(:before).once.ordered
|
||||
callback.should_receive(:before_exit_open).once.ordered # these should be before the state changes
|
||||
callback.should_receive(:before_enter_closed).once.ordered
|
||||
callback.should_receive(:enter_closed).once.ordered
|
||||
callback.should_receive(:aasm_write_state).once.ordered.and_return(true) # this is when the state changes
|
||||
callback.should_receive(:after_exit_open).once.ordered # these should be after the state changes
|
||||
callback.should_receive(:after_enter_closed).once.ordered
|
||||
callback.should_receive(:after).once.ordered
|
||||
|
||||
@callback.close!
|
||||
end
|
||||
|
||||
it "should get open callbacks" do
|
||||
@callback.close!
|
||||
|
||||
@callback.should_receive(:before).once.ordered
|
||||
@callback.should_receive(:before_exit_closed).once.ordered # these should be before the state changes
|
||||
@callback.should_receive(:before_enter_open).once.ordered
|
||||
@callback.should_receive(:aasm_write_state).once.ordered.and_return(true) # this is when the state changes
|
||||
@callback.should_receive(:after_exit_closed).once.ordered # these should be after the state changes
|
||||
@callback.should_receive(:after_enter_open).once.ordered
|
||||
@callback.should_receive(:after).once.ordered
|
||||
|
||||
@callback.open!
|
||||
callback.close!
|
||||
end
|
||||
end
|
||||
|
||||
describe AASM, '- event callbacks' do
|
||||
describe 'callbacks for the new DSL' do
|
||||
let(:callback) {CallbackNewDsl.new}
|
||||
|
||||
it "be called in order" do
|
||||
callback.should_receive(:exit_open).once.ordered
|
||||
callback.should_receive(:before).once.ordered
|
||||
callback.should_receive(:before_exit_open).once.ordered # these should be before the state changes
|
||||
callback.should_receive(:before_enter_closed).once.ordered
|
||||
callback.should_receive(:enter_closed).once.ordered
|
||||
callback.should_receive(:aasm_write_state).once.ordered.and_return(true) # this is when the state changes
|
||||
callback.should_receive(:after_exit_open).once.ordered # these should be after the state changes
|
||||
callback.should_receive(:after_enter_closed).once.ordered
|
||||
callback.should_receive(:after).once.ordered
|
||||
|
||||
callback.close!
|
||||
end
|
||||
end
|
||||
|
||||
describe 'event callbacks' do
|
||||
describe "with an error callback defined" do
|
||||
before do
|
||||
class Foo
|
||||
|
@ -45,10 +49,11 @@ describe AASM, '- event callbacks' do
|
|||
end
|
||||
|
||||
it "should run error_callback if an exception is raised and error_callback defined" do
|
||||
def @foo.error_callback(e)
|
||||
end
|
||||
def @foo.error_callback(e); end
|
||||
|
||||
@foo.stub!(:enter).and_raise(e=StandardError.new)
|
||||
@foo.should_receive(:error_callback).with(e)
|
||||
|
||||
@foo.safe_close!
|
||||
end
|
||||
|
||||
|
@ -66,8 +71,7 @@ describe AASM, '- event callbacks' do
|
|||
describe "with aasm_event_fired defined" do
|
||||
before do
|
||||
@foo = Foo.new
|
||||
def @foo.aasm_event_fired(event, from, to)
|
||||
end
|
||||
def @foo.aasm_event_fired(event, from, to); end
|
||||
end
|
||||
|
||||
it 'should call it for successful bang fire' do
|
||||
|
@ -90,8 +94,7 @@ describe AASM, '- event callbacks' do
|
|||
describe "with aasm_event_failed defined" do
|
||||
before do
|
||||
@foo = Foo.new
|
||||
def @foo.aasm_event_failed(event, from)
|
||||
end
|
||||
def @foo.aasm_event_failed(event, from); end
|
||||
end
|
||||
|
||||
it 'should call it when transition failed for bang fire' do
|
Loading…
Reference in a new issue