From 292138f823dd545c8282e7afb175b296a0042136 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorsten=20Bo=CC=88ttger?= Date: Tue, 4 Dec 2012 16:27:47 +1300 Subject: [PATCH] cleaning up specs --- spec/spec_helpers/models_spec_helper.rb | 13 ------ spec/unit/aasm_spec.rb | 49 ----------------------- spec/unit/event_spec.rb | 11 +++++ spec/unit/initial_state_spec.rb | 28 +++++++++++++ spec/unit/new_dsl_spec.rb | 14 ------- spec/unit/simple_example_spec.rb | 53 +++++++++++++++++++++++++ 6 files changed, 92 insertions(+), 76 deletions(-) delete mode 100644 spec/unit/aasm_spec.rb create mode 100644 spec/unit/initial_state_spec.rb create mode 100644 spec/unit/simple_example_spec.rb diff --git a/spec/spec_helpers/models_spec_helper.rb b/spec/spec_helpers/models_spec_helper.rb index 47ffe81..504a6a7 100644 --- a/spec/spec_helpers/models_spec_helper.rb +++ b/spec/spec_helpers/models_spec_helper.rb @@ -51,19 +51,6 @@ end class Baz < Bar 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 include AASM diff --git a/spec/unit/aasm_spec.rb b/spec/unit/aasm_spec.rb deleted file mode 100644 index 91ebdb9..0000000 --- a/spec/unit/aasm_spec.rb +++ /dev/null @@ -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 - diff --git a/spec/unit/event_spec.rb b/spec/unit/event_spec.rb index debd33e..0b1e11c 100644 --- a/spec/unit/event_spec.rb +++ b/spec/unit/event_spec.rb @@ -191,3 +191,14 @@ describe 'parametrised events' do pe.dress!(:prettying_up) 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 diff --git a/spec/unit/initial_state_spec.rb b/spec/unit/initial_state_spec.rb new file mode 100644 index 0000000..f5afef9 --- /dev/null +++ b/spec/unit/initial_state_spec.rb @@ -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 diff --git a/spec/unit/new_dsl_spec.rb b/spec/unit/new_dsl_spec.rb index 9ed2f49..a6b8b8c 100644 --- a/spec/unit/new_dsl_spec.rb +++ b/spec/unit/new_dsl_spec.rb @@ -4,20 +4,6 @@ describe "the new dsl" do 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 lambda {ProcessWithNewDsl.state}.should raise_error(RuntimeError, "wrong state method") lambda {ProcessWithNewDsl.event}.should raise_error(RuntimeError, "wrong event method") diff --git a/spec/unit/simple_example_spec.rb b/spec/unit/simple_example_spec.rb new file mode 100644 index 0000000..6265a72 --- /dev/null +++ b/spec/unit/simple_example_spec.rb @@ -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