1
0
Fork 0
mirror of https://github.com/aasm/aasm synced 2023-03-27 23:22:41 -04:00

using real database connections for testing to reduce dependency on Rails version (previously mocked away ActiveRecord connection, which is not used in Rails 3.1 anymore)

This commit is contained in:
Thorsten Böttger 2011-08-31 23:49:09 +02:00
parent f93ce4e09a
commit cf18161abc
6 changed files with 76 additions and 51 deletions

2
.gitignore vendored
View file

@ -6,3 +6,5 @@ coverage
pkg pkg
rdoc rdoc
Gemfile.lock Gemfile.lock
spec/debug.log
spec/*.db

View file

@ -11,12 +11,13 @@ Gem::Specification.new do |s|
s.summary = %q{State machine mixin for Ruby objects} s.summary = %q{State machine mixin for Ruby objects}
s.description = %q{AASM is a continuation of the acts as state machine rails plugin, built for plain Ruby objects.} s.description = %q{AASM is a continuation of the acts as state machine rails plugin, built for plain Ruby objects.}
s.add_dependency 'activerecord' #, '~> 3.0' s.add_dependency 'activerecord'
s.add_development_dependency 'rake' s.add_development_dependency 'rake'
s.add_development_dependency 'sdoc' s.add_development_dependency 'sdoc'
s.add_development_dependency 'rspec', '~> 2.0' s.add_development_dependency 'rspec', '~> 2.0'
s.add_development_dependency 'rr' s.add_development_dependency 'rr'
s.add_development_dependency 'shoulda' s.add_development_dependency 'shoulda'
s.add_development_dependency 'sqlite3'
s.add_development_dependency 'minitest' s.add_development_dependency 'minitest'
s.files = `git ls-files`.split("\n") s.files = `git ls-files`.split("\n")

3
spec/database.yml Normal file
View file

@ -0,0 +1,3 @@
sqlite3:
adapter: sqlite3
database: spec/aasm.sqlite3.db

21
spec/schema.rb Normal file
View file

@ -0,0 +1,21 @@
ActiveRecord::Schema.define(:version => 0) do
create_table :gates, :force => true do |t|
end
create_table :readers, :force => true do |t|
end
create_table :writers, :force => true do |t|
end
create_table :transients, :force => true do |t|
end
create_table :simples, :force => true do |t|
end
create_table :thieves, :force => true do |t|
end
end

View file

@ -8,3 +8,10 @@ require 'rspec/autorun'
RSpec.configure do |config| RSpec.configure do |config|
end end
def load_schema
config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
ActiveRecord::Base.establish_connection(config['sqlite3'])
load(File.dirname(__FILE__) + "/schema.rb")
end

View file

@ -2,20 +2,18 @@ begin
require 'rubygems' require 'rubygems'
require 'active_record' require 'active_record'
require 'logger' require 'logger'
load_schema
ActiveRecord::Base.logger = Logger.new(STDERR) ActiveRecord::Base.logger = Logger.new(STDERR)
# A dummy class for mocking the activerecord connection class class Gate < ActiveRecord::Base
class Connection
end
class FooBar < ActiveRecord::Base
include AASM include AASM
# Fake this column for testing purposes # Fake this column for testing purposes
attr_accessor :aasm_state attr_accessor :aasm_state
aasm_state :open aasm_state :opened
aasm_state :closed aasm_state :closed
aasm_event :view do aasm_event :view do
@ -23,36 +21,37 @@ begin
end end
end end
class Fi < ActiveRecord::Base class Reader < ActiveRecord::Base
def aasm_read_state def aasm_read_state
"fi" "fi"
end end
include AASM include AASM
end end
class Fo < ActiveRecord::Base class Writer < ActiveRecord::Base
def aasm_write_state(state) def aasm_write_state(state)
"fo" "fo"
end end
include AASM include AASM
end end
class Fum < ActiveRecord::Base class Transient < ActiveRecord::Base
def aasm_write_state_without_persistence(state) def aasm_write_state_without_persistence(state)
"fum" "fum"
end end
include AASM include AASM
end end
class June < ActiveRecord::Base class Simple < ActiveRecord::Base
include AASM include AASM
aasm_column :status aasm_column :status
end end
class Beaver < June class Derivate < Simple
end end
class Thief < ActiveRecord::Base class Thief < ActiveRecord::Base
set_table_name "thieves"
include AASM include AASM
aasm_initial_state Proc.new { |thief| thief.skilled ? :rich : :jailed } aasm_initial_state Proc.new { |thief| thief.skilled ? :rich : :jailed }
aasm_state :rich aasm_state :rich
@ -69,9 +68,9 @@ begin
end end
end end
describe FooBar, "class methods" do describe Gate, "class methods" do
before(:each) do before(:each) do
@klass = FooBar @klass = Gate
end end
it_should_behave_like "aasm model" it_should_behave_like "aasm model"
it "should include AASM::Persistence::ActiveRecordPersistence::ReadState" do it "should include AASM::Persistence::ActiveRecordPersistence::ReadState" do
@ -85,9 +84,9 @@ begin
end end
end end
describe Fi, "class methods" do describe Reader, "class methods" do
before(:each) do before(:each) do
@klass = Fi @klass = Reader
end end
it_should_behave_like "aasm model" it_should_behave_like "aasm model"
it "should not include AASM::Persistence::ActiveRecordPersistence::ReadState" do it "should not include AASM::Persistence::ActiveRecordPersistence::ReadState" do
@ -101,9 +100,9 @@ begin
end end
end end
describe Fo, "class methods" do describe Writer, "class methods" do
before(:each) do before(:each) do
@klass = Fo @klass = Writer
end end
it_should_behave_like "aasm model" it_should_behave_like "aasm model"
it "should include AASM::Persistence::ActiveRecordPersistence::ReadState" do it "should include AASM::Persistence::ActiveRecordPersistence::ReadState" do
@ -117,9 +116,9 @@ begin
end end
end end
describe Fum, "class methods" do describe Transient, "class methods" do
before(:each) do before(:each) do
@klass = Fum @klass = Transient
end end
it_should_behave_like "aasm model" it_should_behave_like "aasm model"
it "should include AASM::Persistence::ActiveRecordPersistence::ReadState" do it "should include AASM::Persistence::ActiveRecordPersistence::ReadState" do
@ -133,61 +132,57 @@ begin
end end
end end
describe FooBar, "instance methods" do describe Gate, "instance methods" do
before(:each) do
connection = mock(Connection, :columns => [])
FooBar.stub!(:connection).and_return(connection)
end
it "should respond to aasm read state when not previously defined" do it "should respond to aasm read state when not previously defined" do
FooBar.new.should respond_to(:aasm_read_state) Gate.new.should respond_to(:aasm_read_state)
end end
it "should respond to aasm write state when not previously defined" do it "should respond to aasm write state when not previously defined" do
FooBar.new.should respond_to(:aasm_write_state) Gate.new.should respond_to(:aasm_write_state)
end end
it "should respond to aasm write state without persistence when not previously defined" do it "should respond to aasm write state without persistence when not previously defined" do
FooBar.new.should respond_to(:aasm_write_state_without_persistence) Gate.new.should respond_to(:aasm_write_state_without_persistence)
end end
it "should return the initial state when new and the aasm field is nil" do it "should return the initial state when new and the aasm field is nil" do
FooBar.new.aasm_current_state.should == :open Gate.new.aasm_current_state.should == :opened
end end
it "should return the aasm column when new and the aasm field is not nil" do it "should return the aasm column when new and the aasm field is not nil" do
foo = FooBar.new foo = Gate.new
foo.aasm_state = "closed" foo.aasm_state = "closed"
foo.aasm_current_state.should == :closed foo.aasm_current_state.should == :closed
end end
it "should return the aasm column when not new and the aasm_column is not nil" do it "should return the aasm column when not new and the aasm_column is not nil" do
foo = FooBar.new foo = Gate.new
foo.stub!(:new_record?).and_return(false) foo.stub!(:new_record?).and_return(false)
foo.aasm_state = "state" foo.aasm_state = "state"
foo.aasm_current_state.should == :state foo.aasm_current_state.should == :state
end end
it "should allow a nil state" do it "should allow a nil state" do
foo = FooBar.new foo = Gate.new
foo.stub!(:new_record?).and_return(false) foo.stub!(:new_record?).and_return(false)
foo.aasm_state = nil foo.aasm_state = nil
foo.aasm_current_state.should be_nil foo.aasm_current_state.should be_nil
end end
it "should have aasm_ensure_initial_state" do it "should have aasm_ensure_initial_state" do
foo = FooBar.new foo = Gate.new
foo.send :aasm_ensure_initial_state foo.send :aasm_ensure_initial_state
end end
it "should call aasm_ensure_initial_state on validation before create" do it "should call aasm_ensure_initial_state on validation before create" do
foo = FooBar.new foo = Gate.new
foo.should_receive(:aasm_ensure_initial_state).and_return(true) foo.should_receive(:aasm_ensure_initial_state).and_return(true)
foo.valid? foo.valid?
end end
it "should call aasm_ensure_initial_state on validation before create" do it "should call aasm_ensure_initial_state on validation before create" do
foo = FooBar.new foo = Gate.new
foo.stub!(:new_record?).and_return(false) foo.stub!(:new_record?).and_return(false)
foo.should_not_receive(:aasm_ensure_initial_state) foo.should_not_receive(:aasm_ensure_initial_state)
foo.valid? foo.valid?
@ -195,45 +190,41 @@ begin
end end
describe 'Beavers' do describe 'Derivates' do
it "should have the same states as it's parent" do it "should have the same states as it's parent" do
Beaver.aasm_states.should == June.aasm_states Derivate.aasm_states.should == Simple.aasm_states
end end
it "should have the same events as it's parent" do it "should have the same events as it's parent" do
Beaver.aasm_events.should == June.aasm_events Derivate.aasm_events.should == Simple.aasm_events
end end
it "should have the same column as it's parent" do it "should have the same column as it's parent" do
Beaver.aasm_column.should == :status Derivate.aasm_column.should == :status
end end
end end
describe AASM::Persistence::ActiveRecordPersistence::NamedScopeMethods do describe AASM::Persistence::ActiveRecordPersistence::NamedScopeMethods do
class NamedScopeExample < ActiveRecord::Base
include AASM
end
context "Does not already respond_to? the scope name" do context "Does not already respond_to? the scope name" do
it "should add a scope" do it "should add a scope" do
NamedScopeExample.aasm_state :unknown_scope Simple.should_not respond_to(:unknown_scope)
NamedScopeExample.scopes.keys.should include(:unknown_scope) Simple.aasm_state :unknown_scope
Simple.should respond_to(:unknown_scope)
Simple.unknown_scope.class.should == ActiveRecord::Relation
end end
end end
context "Already respond_to? the scope name" do context "Already respond_to? the scope name" do
it "should not add a scope" do it "should not add a scope" do
NamedScopeExample.aasm_state :new Simple.aasm_state :new
NamedScopeExample.scopes.keys.should_not include(:new) Simple.should respond_to(:new)
Simple.new.class.should == Simple
end end
end end
end end
describe 'Thieves' do describe 'Thieves' do
before(:each) do
connection = mock(Connection, :columns => [])
Thief.stub!(:connection).and_return(connection)
end
it 'should be rich if they\'re skilled' do it 'should be rich if they\'re skilled' do
Thief.new(:skilled => true).aasm_current_state.should == :rich Thief.new(:skilled => true).aasm_current_state.should == :rich