diff --git a/spec/schema.rb b/spec/schema.rb index 298c0b5..30e781d 100644 --- a/spec/schema.rb +++ b/spec/schema.rb @@ -4,4 +4,9 @@ ActiveRecord::Schema.define(:version => 0) do create_table table_name, :force => true end + create_table "validators", :force => true do |t| + t.string "name" + t.string "status" + end + end diff --git a/spec/unit/aasm_spec.rb b/spec/unit/aasm_spec.rb index 5f6f792..ee4d921 100644 --- a/spec/unit/aasm_spec.rb +++ b/spec/unit/aasm_spec.rb @@ -103,75 +103,16 @@ describe AASM, '- initial states' do end end -describe AASM, '- event firing with persistence' do - it 'should update the current state' do - foo = Foo.new - foo.close! - - foo.aasm_current_state.should == :closed - end - +describe AASM, 'success callbacks' do it 'should call the success callback if one was provided' do foo = Foo.new - foo.should_receive(:success_callback) - foo.close! end - 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) - - foo.close! - end - - it 'should return true if aasm_write_state is defined and returns true' do - foo = Foo.new - - def foo.aasm_write_state(state) - true - end - - foo.close!.should be_true - end - - it 'should return false if aasm_write_state is defined and returns false' do - foo = Foo.new - - def foo.aasm_write_state(state) - false - end - - foo.close!.should be_false - end - - it "should not update the aasm_current_state if the write fails" do - foo = Foo.new - - def foo.aasm_write_state - false - end - - foo.should_receive(:aasm_write_state) - - foo.close! - foo.aasm_current_state.should == :open - end end describe AASM, '- event firing without persistence' do - it 'should update the current state' do - foo = Foo.new - foo.close - - foo.aasm_current_state.should == :closed - end - it 'should attempt to persist if aasm_write_state is defined' do foo = Foo.new @@ -184,18 +125,6 @@ describe AASM, '- event firing without persistence' do end end -describe AASM, '- persistence' do - it 'should read the state if it has not been set and aasm_read_state is defined' do - foo = Foo.new - def foo.aasm_read_state - end - - foo.should_receive(:aasm_read_state) - - foo.aasm_current_state - end -end - describe AASM, '- getting events for a state' do it '#aasm_events_for_current_state should use current state' do foo = Foo.new diff --git a/spec/unit/active_record_persistence_spec.rb b/spec/unit/active_record_persistence_spec.rb index 4739df3..437fcf2 100644 --- a/spec/unit/active_record_persistence_spec.rb +++ b/spec/unit/active_record_persistence_spec.rb @@ -68,6 +68,21 @@ class Thief < ActiveRecord::Base attr_accessor :skilled, :aasm_state end +class Validator < ActiveRecord::Base + include AASM + aasm :column => :status do + state :sleeping, :initial => true + state :running + event :run do + transitions :to => :running, :from => :sleeping + end + event :sleep do + transitions :to => :sleeping, :from => :running + end + end + validates_presence_of :name +end + shared_examples_for "aasm model" do it "should include AASM::Persistence::ActiveRecordPersistence" do @klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence) @@ -246,3 +261,31 @@ describe 'Thieves' do Thief.new(:skilled => false).aasm_current_state.should == :jailed end end + +describe 'transitions with persistence' do + + it 'should succeed and store the new state' do + validator = Validator.create(:name => 'name') + validator.should be_valid + validator.should be_sleeping + + # validator.name = nil + # validator.should_not be_valid + # validator.run!.should be_false + # validator.should be_running + # + # validator.reload + # validator.should_not be_running + # validator.should be_sleeping + + validator.name = 'another name' + validator.should be_valid + validator.run!.should be_true + validator.should be_running + + validator.reload + validator.should be_running + validator.should_not be_sleeping + end + +end diff --git a/spec/unit/localizer_spec.rb b/spec/unit/localizer_spec.rb index 5b2a69c..4f471cc 100644 --- a/spec/unit/localizer_spec.rb +++ b/spec/unit/localizer_spec.rb @@ -31,7 +31,7 @@ describe AASM::SupportingClasses::Localizer do context 'aasm_human_state' do it 'should return translated state value' do - foo_opened.aasm_human_state.should == "It's opened now!" + foo_opened.aasm_human_state.should == "It's opened now!" end it 'should return humanized value if not localized' do