This commit is contained in:
Thorsten Böttger 2011-12-29 16:23:48 +01:00
parent ee9b1cd19c
commit 996f7e9675
4 changed files with 50 additions and 73 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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