Sha256: d83d30dcfef25187c2da4f54cf08094641f7e58c8060eb1342b4023e951c6959

Contents?: true

Size: 1.52 KB

Versions: 8

Compression:

Stored size: 1.52 KB

Contents

require 'test_helper'

class Invoice
  attr_accessor(:state, :amount)
  include FSM
  define_fsm do
    state_attribute(:state) 
    state(:open)
    state(:paid)
    state(:refunded)
    
    transition(:pay, :open, :paid, :event => :event_paid)
    transition(:refund, :paid, :refunded)
  end
  
  
  def initialize(state = :open, amount = 1000)
    self.state = state
    self.amount = amount
  end
  
  private
  def event_paid(amount_paid)
    raise "Not enough paid" unless amount_paid == self.amount
    self.amount -= amount_paid
  end
end

class InvoiceSampleTest < Test::Unit::TestCase
  context 'Invoice' do
    
    should 'Initial State is the first state defined unless no initial() call was made' do
      invoice = Invoice.new
      assert_equal(:open, invoice.state)
    end
    
    should 'Accept an initial state from outside' do
      invoice = Invoice.new(:paid)
      assert_equal(:paid, invoice.state)
    end
    
    should 'Trasition to paid and then refunded' do
      invoice = Invoice.new
      assert_equal(:open, invoice.state)
      
      invoice.pay(1000)
      assert_equal(:paid, invoice.state)
      
      invoice.refund
      assert_equal(:refunded, invoice.state)
    end
    
    should 'Raise on illegal transition' do
      invoice = Invoice.new
      assert_raise(FSM::InvalidStateTransition) do
        invoice.refund
      end
    end
    
    should 'Pass the arguments to the event handler' do
      invoice = Invoice.new
      invoice.pay(1000)
      assert_equal(0, invoice.amount)
    end
    
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
simplificator-fsm-0.2.1 test/invoice_sample_test.rb
simplificator-fsm-0.2.2 test/invoice_sample_test.rb
simplificator-fsm-0.2.3 test/invoice_sample_test.rb
simplificator-fsm-0.2.4 test/invoice_sample_test.rb
simplificator-fsm-0.3.0 test/invoice_sample_test.rb
simplificator-fsm-0.3.2 test/invoice_sample_test.rb
simplificator-fsm-0.3.3 test/invoice_sample_test.rb
simplificator-fsm-0.3.5 test/invoice_sample_test.rb