Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: How to test state_machine?

Please, help me. I'm confused. I know how to write state-driven behavior of model, but I don't know what should I write in specs...

My model.rb file look

class Ratification < ActiveRecord::Base
  belongs_to :user

  attr_protected :status_events

  state_machine :status, :initial => :boss do
    state :boss
    state :owner
    state :declarant
    state :done

    event :approve do
      transition :boss => :owner, :owner => :done
    end

    event :divert do
      transition [:boss, :owner] => :declarant
    end

    event :repeat do
      transition :declarant => :boss
    end

  end
end

I use state_machine gem.

Please, show me the course.

like image 514
petRUShka Avatar asked Jun 15 '10 17:06

petRUShka


3 Answers

The question is old, but I had the same one. Taking example from state_machine gem :

class Vehicle
  state_machine :state, :initial => :parked do
    event :park do
      transition [:idling, :first_gear] => :parked
    end

    event :ignite do
      transition :stalled => same, :parked => :idling
    end

    event :idle do
      transition :first_gear => :idling
    end

    event :shift_up do
      transition :idling => :first_gear, :first_gear => :second_gear, :second_gear => :third_gear
    end

    event :shift_down do
      transition :third_gear => :second_gear, :second_gear => :first_gear
    end
  end
end

My solution was:

describe Vehicle do

  before :each do
    @vehicle = Factory(:vehicle)
  end

  describe 'states' do
    describe ':parked' do
      it 'should be an initial state' do
        # Check for @vehicle.parked? to be true
        @vehicle.should be_parked
      end

      it 'should change to :idling on :ignite' do
        @vehicle.ignite!
        @vehicle.should be_idling
      end

      ['shift_up!', 'shift_down!'].each do |action|
        it "should raise an error for #{action}" do
          lambda {@job_offer.send(action)}.should raise_error
        end
      end
    end
  end
end

I was using:

  • ruby (1.9.3)
  • rails (3.1.3)
  • rspec (2.8.0.rc1)
  • factory_girl (2.3.2)
  • state_machine (1.1.0)
like image 190
santuxus Avatar answered Oct 17 '22 16:10

santuxus


The state_machine_rspec gem includes many helper methods for writing concise specs.

 describe Ratification do
   it { should have_states :boss, :declarant, :done, :owner }
   it { should handle_events :approve, when: :boss }
   it { should handle_events :approve, when: :owner }
   it { should handle_events :divert, when: :boss }
   it { should handle_events :divert, when: :owner }
   it { should handle_events :repeat, when: :declarant }
   it { should reject_events :approve, :divert, :repeat, when: :done }
   it { should reject_events :approve, :divert, :repeat, when: :done }
 end

These RSpec matchers will assist with the state_machine specs from a high-level. From here, one needs to write the specs for the business cases for can_approve?, can_divert?, and can_repeat?.

like image 3
scarver2 Avatar answered Oct 17 '22 16:10

scarver2


I have written a RSpec custom matcher. It allows to test state flow in elegant and simple way: check it out

like image 1
ka8725 Avatar answered Oct 17 '22 15:10

ka8725