add a complex example spec for Mongoid

This commit is contained in:
Thorsten Böttger 2015-07-11 22:10:47 +12:00
parent 44cf3f3220
commit 440587c806
3 changed files with 92 additions and 1 deletions

View File

@ -0,0 +1,37 @@
class ComplexMongoidExample
include Mongoid::Document
include AASM
field :left, :type => String
field :right, :type => String
aasm :left, :column => 'left' do
state :one, :initial => true
state :two
state :three
event :increment do
transitions :from => :one, :to => :two
transitions :from => :two, :to => :three
end
event :reset do
transitions :from => :three, :to => :one
end
end
aasm :right, :column => 'right' do
state :alpha, :initial => true
state :beta
state :gamma
event :level_up do
transitions :from => :alpha, :to => :beta
transitions :from => :beta, :to => :gamma
end
event :level_down do
transitions :from => :gamma, :to => :beta
transitions :from => :beta, :to => :alpha
end
end
end

View File

@ -69,7 +69,59 @@ describe 'mongoid' do
end
describe "complex example" do
it "works" do
record = ComplexMongoidExample.new
expect_aasm_states record, :one, :alpha
record.save!
expect_aasm_states record, :one, :alpha
record.reload
expect_aasm_states record, :one, :alpha
record.increment!
expect_aasm_states record, :two, :alpha
record.reload
expect_aasm_states record, :two, :alpha
record.level_up!
expect_aasm_states record, :two, :beta
record.reload
expect_aasm_states record, :two, :beta
record.increment!
expect { record.increment! }.to raise_error(AASM::InvalidTransition)
expect_aasm_states record, :three, :beta
record.reload
expect_aasm_states record, :three, :beta
record.level_up!
expect_aasm_states record, :three, :gamma
record.reload
expect_aasm_states record, :three, :gamma
record.level_down # without saving
expect_aasm_states record, :three, :beta
record.reload
expect_aasm_states record, :three, :gamma
record.level_down # without saving
expect_aasm_states record, :three, :beta
record.reset!
expect_aasm_states record, :one, :beta
end
def expect_aasm_states(record, left_state, right_state)
expect(record.aasm(:left).current_state).to eql left_state.to_sym
expect(record.left).to eql left_state.to_s
expect(record.aasm(:right).current_state).to eql right_state.to_sym
expect(record.right).to eql right_state.to_s
end
end
rescue LoadError
puts "Not running Mongoid specs because mongoid gem is not installed!!!"
puts "--------------------------------------------------------------------------"
puts "Not running Mongoid multiple-specs because mongoid gem is not installed!!!"
puts "--------------------------------------------------------------------------"
end
end

View File

@ -72,6 +72,8 @@ describe 'mongoid' do
end
rescue LoadError
puts "--------------------------------------------------------------------------"
puts "Not running Mongoid specs because mongoid gem is not installed!!!"
puts "--------------------------------------------------------------------------"
end
end