cleaning up specs
This commit is contained in:
parent
5c6e0cd768
commit
292138f823
|
@ -51,19 +51,6 @@ end
|
||||||
class Baz < Bar
|
class Baz < Bar
|
||||||
end
|
end
|
||||||
|
|
||||||
class Banker
|
|
||||||
include AASM
|
|
||||||
aasm do
|
|
||||||
state :retired
|
|
||||||
state :selling_bad_mortgages
|
|
||||||
end
|
|
||||||
aasm_initial_state Proc.new { |banker| banker.rich? ? :retired : :selling_bad_mortgages }
|
|
||||||
RICH = 1_000_000
|
|
||||||
attr_accessor :balance
|
|
||||||
def initialize(balance = 0); self.balance = balance; end
|
|
||||||
def rich?; self.balance >= RICH; end
|
|
||||||
end
|
|
||||||
|
|
||||||
class ThisNameBetterNotBeInUse
|
class ThisNameBetterNotBeInUse
|
||||||
include AASM
|
include AASM
|
||||||
|
|
||||||
|
|
|
@ -1,49 +0,0 @@
|
||||||
require 'spec_helper'
|
|
||||||
|
|
||||||
describe 'instance methods' do
|
|
||||||
let(:foo) {Foo.new}
|
|
||||||
|
|
||||||
it 'should define a state querying instance method on including class' do
|
|
||||||
foo.should respond_to(:open?)
|
|
||||||
foo.should be_open
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'should define an event! instance method' do
|
|
||||||
foo.should respond_to(:close!)
|
|
||||||
foo.close!
|
|
||||||
foo.should be_closed
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe AASM, '- initial states' do
|
|
||||||
let(:foo) {Foo.new}
|
|
||||||
let(:bar) {Bar.new}
|
|
||||||
|
|
||||||
it 'should set the initial state' do
|
|
||||||
foo.aasm_current_state.should == :open
|
|
||||||
# foo.aasm.current_state.should == :open # not yet supported
|
|
||||||
foo.should be_open
|
|
||||||
foo.should_not be_closed
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'should use the first state defined if no initial state is given' do
|
|
||||||
bar.aasm_current_state.should == :read
|
|
||||||
# bar.aasm.current_state.should == :read # not yet supported
|
|
||||||
end
|
|
||||||
|
|
||||||
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
|
|
||||||
end
|
|
||||||
|
|
||||||
describe 'event firing without persistence' do
|
|
||||||
it 'should attempt to persist if aasm_write_state is defined' do
|
|
||||||
foo = Foo.new
|
|
||||||
def foo.aasm_write_state; end
|
|
||||||
|
|
||||||
foo.should_receive(:aasm_write_state_without_persistence).twice
|
|
||||||
foo.close
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
|
@ -191,3 +191,14 @@ describe 'parametrised events' do
|
||||||
pe.dress!(:prettying_up)
|
pe.dress!(:prettying_up)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe 'event firing without persistence' do
|
||||||
|
it 'should attempt to persist if aasm_write_state is defined' do
|
||||||
|
foo = Foo.new
|
||||||
|
def foo.aasm_write_state; end
|
||||||
|
foo.should be_open
|
||||||
|
|
||||||
|
foo.should_receive(:aasm_write_state_without_persistence)
|
||||||
|
foo.close
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
|
@ -0,0 +1,28 @@
|
||||||
|
require 'spec_helper'
|
||||||
|
|
||||||
|
class Banker
|
||||||
|
include AASM
|
||||||
|
aasm do
|
||||||
|
state :retired
|
||||||
|
state :selling_bad_mortgages
|
||||||
|
end
|
||||||
|
aasm_initial_state Proc.new { |banker| banker.rich? ? :retired : :selling_bad_mortgages }
|
||||||
|
RICH = 1_000_000
|
||||||
|
attr_accessor :balance
|
||||||
|
def initialize(balance = 0); self.balance = balance; end
|
||||||
|
def rich?; self.balance >= RICH; end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'initial states' do
|
||||||
|
let(:bar) {Bar.new}
|
||||||
|
|
||||||
|
it 'should use the first state defined if no initial state is given' do
|
||||||
|
bar.aasm_current_state.should == :read
|
||||||
|
# bar.aasm.current_state.should == :read # not yet supported
|
||||||
|
end
|
||||||
|
|
||||||
|
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
|
||||||
|
end
|
|
@ -4,20 +4,6 @@ describe "the new dsl" do
|
||||||
|
|
||||||
let(:process) {ProcessWithNewDsl.new}
|
let(:process) {ProcessWithNewDsl.new}
|
||||||
|
|
||||||
it 'should use an initial event' do
|
|
||||||
process.aasm_current_state.should == :sleeping
|
|
||||||
process.should be_sleeping
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'should have states and transitions' do
|
|
||||||
process.flagged.should be_nil
|
|
||||||
process.start!
|
|
||||||
process.should be_running
|
|
||||||
process.flagged.should be_true
|
|
||||||
process.stop!
|
|
||||||
process.should be_suspended
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'should not conflict with other event or state methods' do
|
it 'should not conflict with other event or state methods' do
|
||||||
lambda {ProcessWithNewDsl.state}.should raise_error(RuntimeError, "wrong state method")
|
lambda {ProcessWithNewDsl.state}.should raise_error(RuntimeError, "wrong state method")
|
||||||
lambda {ProcessWithNewDsl.event}.should raise_error(RuntimeError, "wrong event method")
|
lambda {ProcessWithNewDsl.event}.should raise_error(RuntimeError, "wrong event method")
|
||||||
|
|
|
@ -0,0 +1,53 @@
|
||||||
|
require 'spec_helper'
|
||||||
|
|
||||||
|
class Payment
|
||||||
|
include AASM
|
||||||
|
aasm do
|
||||||
|
state :initialised, :initial => true
|
||||||
|
state :filled_out
|
||||||
|
state :authorised
|
||||||
|
|
||||||
|
event :fill_out do
|
||||||
|
transitions :from => :initialised, :to => :filled_out
|
||||||
|
end
|
||||||
|
event :authorise do
|
||||||
|
transitions :from => :filled_out, :to => :authorised
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'state machine' do
|
||||||
|
let(:payment) {Payment.new}
|
||||||
|
|
||||||
|
it 'starts with an initial state' do
|
||||||
|
payment.aasm_current_state.should == :initialised
|
||||||
|
# payment.aasm.current_state.should == :initialised # not yet supported
|
||||||
|
payment.should respond_to(:initialised?)
|
||||||
|
payment.should be_initialised
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'allows transitions to other states' do
|
||||||
|
payment.should respond_to(:fill_out)
|
||||||
|
payment.should respond_to(:fill_out!)
|
||||||
|
payment.fill_out!
|
||||||
|
payment.should respond_to(:filled_out?)
|
||||||
|
payment.should be_filled_out
|
||||||
|
|
||||||
|
payment.should respond_to(:authorise)
|
||||||
|
payment.should respond_to(:authorise!)
|
||||||
|
payment.authorise
|
||||||
|
payment.should respond_to(:authorised?)
|
||||||
|
payment.should be_authorised
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'denies transitions to other states' do
|
||||||
|
lambda {payment.authorise}.should raise_error(AASM::InvalidTransition)
|
||||||
|
lambda {payment.authorise!}.should raise_error(AASM::InvalidTransition)
|
||||||
|
payment.fill_out
|
||||||
|
lambda {payment.fill_out}.should raise_error(AASM::InvalidTransition)
|
||||||
|
lambda {payment.fill_out!}.should raise_error(AASM::InvalidTransition)
|
||||||
|
payment.authorise
|
||||||
|
lambda {payment.fill_out}.should raise_error(AASM::InvalidTransition)
|
||||||
|
lambda {payment.fill_out!}.should raise_error(AASM::InvalidTransition)
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in New Issue