2008-04-29 05:27:56 +00:00
|
|
|
require File.join(File.dirname(__FILE__), '..', '..', 'lib', 'aasm')
|
|
|
|
|
|
|
|
begin
|
|
|
|
require 'active_record'
|
|
|
|
|
|
|
|
# A dummy class for mocking the activerecord connection class
|
|
|
|
class Connection
|
|
|
|
end
|
|
|
|
|
|
|
|
class Foo < ActiveRecord::Base
|
|
|
|
include AASM
|
|
|
|
|
|
|
|
# Fake this column for testing purposes
|
|
|
|
attr_accessor :aasm_state
|
|
|
|
|
|
|
|
aasm_state :open
|
|
|
|
aasm_state :closed
|
|
|
|
|
|
|
|
aasm_event :view do
|
|
|
|
transitions :to => :read, :from => [:needs_attention]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Fi < ActiveRecord::Base
|
|
|
|
def aasm_read_state
|
|
|
|
"fi"
|
|
|
|
end
|
|
|
|
include AASM
|
|
|
|
end
|
|
|
|
|
|
|
|
class Fo < ActiveRecord::Base
|
|
|
|
def aasm_write_state(state)
|
|
|
|
"fo"
|
|
|
|
end
|
|
|
|
include AASM
|
|
|
|
end
|
|
|
|
|
|
|
|
class Fum < ActiveRecord::Base
|
|
|
|
def aasm_write_state_without_persistence(state)
|
|
|
|
"fum"
|
|
|
|
end
|
|
|
|
include AASM
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "aasm model", :shared => true do
|
|
|
|
it "should include AASM::Persistence::ActiveRecordPersistence" do
|
|
|
|
@klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence)
|
|
|
|
end
|
|
|
|
it "should include AASM::Persistence::ActiveRecordPersistence::InstanceMethods" do
|
|
|
|
@klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::InstanceMethods)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe Foo, "class methods" do
|
|
|
|
before(:each) do
|
|
|
|
@klass = Foo
|
|
|
|
end
|
|
|
|
it_should_behave_like "aasm model"
|
|
|
|
it "should include AASM::Persistence::ActiveRecordPersistence::ReadState" do
|
|
|
|
@klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::ReadState)
|
|
|
|
end
|
|
|
|
it "should include AASM::Persistence::ActiveRecordPersistence::WriteState" do
|
|
|
|
@klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::WriteState)
|
|
|
|
end
|
|
|
|
it "should include AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence" do
|
|
|
|
@klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe Fi, "class methods" do
|
|
|
|
before(:each) do
|
|
|
|
@klass = Fi
|
|
|
|
end
|
|
|
|
it_should_behave_like "aasm model"
|
|
|
|
it "should not include AASM::Persistence::ActiveRecordPersistence::ReadState" do
|
|
|
|
@klass.included_modules.should_not be_include(AASM::Persistence::ActiveRecordPersistence::ReadState)
|
|
|
|
end
|
|
|
|
it "should include AASM::Persistence::ActiveRecordPersistence::WriteState" do
|
|
|
|
@klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::WriteState)
|
|
|
|
end
|
|
|
|
it "should include AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence" do
|
|
|
|
@klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe Fo, "class methods" do
|
|
|
|
before(:each) do
|
|
|
|
@klass = Fo
|
|
|
|
end
|
|
|
|
it_should_behave_like "aasm model"
|
|
|
|
it "should include AASM::Persistence::ActiveRecordPersistence::ReadState" do
|
|
|
|
@klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::ReadState)
|
|
|
|
end
|
|
|
|
it "should not include AASM::Persistence::ActiveRecordPersistence::WriteState" do
|
|
|
|
@klass.included_modules.should_not be_include(AASM::Persistence::ActiveRecordPersistence::WriteState)
|
|
|
|
end
|
|
|
|
it "should include AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence" do
|
|
|
|
@klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe Fum, "class methods" do
|
|
|
|
before(:each) do
|
|
|
|
@klass = Fum
|
|
|
|
end
|
|
|
|
it_should_behave_like "aasm model"
|
|
|
|
it "should include AASM::Persistence::ActiveRecordPersistence::ReadState" do
|
|
|
|
@klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::ReadState)
|
|
|
|
end
|
|
|
|
it "should include AASM::Persistence::ActiveRecordPersistence::WriteState" do
|
|
|
|
@klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::WriteState)
|
|
|
|
end
|
|
|
|
it "should not include AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence" do
|
|
|
|
@klass.included_modules.should_not be_include(AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe Foo, "instance methods" do
|
|
|
|
before(:each) do
|
|
|
|
connection = mock(Connection, :columns => [])
|
|
|
|
Foo.stub!(:connection).and_return(connection)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should respond to aasm read state when not previously defined" do
|
|
|
|
Foo.new.should respond_to(:aasm_read_state)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should respond to aasm write state when not previously defined" do
|
|
|
|
Foo.new.should respond_to(:aasm_write_state)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should respond to aasm write state without persistence when not previously defined" do
|
|
|
|
Foo.new.should respond_to(:aasm_write_state_without_persistence)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should return the initial state when new and the aasm field is nil" do
|
|
|
|
Foo.new.aasm_current_state.should == :open
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should return the aasm column when new and the aasm field is not nil" do
|
|
|
|
foo = Foo.new
|
|
|
|
foo.aasm_state = "closed"
|
|
|
|
foo.aasm_current_state.should == :closed
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should return the aasm column when not new and the aasm_column is not nil" do
|
|
|
|
foo = Foo.new
|
|
|
|
foo.stub!(:new_record?).and_return(false)
|
|
|
|
foo.aasm_state = "state"
|
|
|
|
foo.aasm_current_state.should == :state
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should allow a nil state" do
|
|
|
|
foo = Foo.new
|
|
|
|
foo.stub!(:new_record?).and_return(false)
|
|
|
|
foo.aasm_state = nil
|
|
|
|
foo.aasm_current_state.should be_nil
|
|
|
|
end
|
|
|
|
|
2008-04-29 05:58:05 +00:00
|
|
|
it "should have aasm_ensure_initial_state" do
|
|
|
|
foo = Foo.new
|
|
|
|
foo.send :aasm_ensure_initial_state
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should call aasm_ensure_initial_state on validation before create" do
|
|
|
|
foo = Foo.new
|
|
|
|
foo.should_receive(:aasm_ensure_initial_state).and_return(true)
|
|
|
|
foo.valid?
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should call aasm_ensure_initial_state on validation before create" do
|
|
|
|
foo = Foo.new
|
|
|
|
foo.stub!(:new_record?).and_return(false)
|
|
|
|
foo.should_not_receive(:aasm_ensure_initial_state)
|
|
|
|
foo.valid?
|
|
|
|
end
|
|
|
|
|
2008-04-29 05:27:56 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# TODO: figure out how to test ActiveRecord reload! without a database
|
|
|
|
|
|
|
|
rescue LoadError => e
|
|
|
|
if e.message == "no such file to load -- active_record"
|
|
|
|
puts "You must install active record to run this spec. Install with sudo gem install activerecord"
|
|
|
|
else
|
|
|
|
raise
|
|
|
|
end
|
|
|
|
end
|