Skip to content

Commit

Permalink
add a complex example spec for Mongoid
Browse files Browse the repository at this point in the history
  • Loading branch information
alto committed Jul 11, 2015
1 parent 44cf3f3 commit 440587c
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 1 deletion.
37 changes: 37 additions & 0 deletions spec/models/mongoid/complex_mongoid_example.rb
@@ -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
54 changes: 53 additions & 1 deletion spec/unit/persistence/mongoid_persistence_multiple_spec.rb
Expand Up @@ -69,7 +69,59 @@

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
2 changes: 2 additions & 0 deletions spec/unit/persistence/mongoid_persistence_spec.rb
Expand Up @@ -72,6 +72,8 @@
end

rescue LoadError
puts "--------------------------------------------------------------------------"
puts "Not running Mongoid specs because mongoid gem is not installed!!!"
puts "--------------------------------------------------------------------------"
end
end

0 comments on commit 440587c

Please sign in to comment.