File: machine_with_initialized_state_test.rb

package info (click to toggle)
ruby-state-machines-activerecord 0.103.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 432 kB
  • sloc: ruby: 2,527; makefile: 6
file content (43 lines) | stat: -rw-r--r-- 1,112 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# frozen_string_literal: true

require_relative 'test_helper'

class MachineWithInitializedStateTest < BaseTestCase
  def setup
    @model = new_model
    @machine = StateMachines::Machine.new(@model, initial: :parked)
    @machine.state :idling
  end

  def test_should_allow_nil_initial_state_when_static
    @machine.state nil

    record = @model.new(state: nil)
    assert_nil record.state
  end

  def test_should_allow_nil_initial_state_when_dynamic
    @machine.state nil

    @machine.initial_state = -> { :parked }
    record = @model.new(state: nil)
    assert_nil record.state
  end

  def test_should_allow_different_initial_state_when_static
    record = @model.new(state: 'idling')
    assert_equal 'idling', record.state
  end

  def test_should_allow_different_initial_state_when_using_create_with
    record = @model.create_with(state: 'idling').new

    assert_equal 'idling', record.state
  end

  def test_should_allow_different_initial_state_when_dynamic
    @machine.initial_state = -> { :parked }
    record = @model.new(state: 'idling')
    assert_equal 'idling', record.state
  end
end