File: machine_with_failed_before_callbacks_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 (45 lines) | stat: -rw-r--r-- 1,121 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
44
45
# frozen_string_literal: true

require_relative 'test_helper'

class MachineWithFailedBeforeCallbacksTest < BaseTestCase
  def setup
    @callbacks = []

    @model = new_model
    @machine = StateMachines::Machine.new(@model)
    @machine.state :parked, :idling
    @machine.event :ignite
    @machine.before_transition do
      @callbacks << :before_1
      false
    end
    @machine.before_transition { @callbacks << :before_2 }
    @machine.after_transition { @callbacks << :after }
    @machine.around_transition do |block|
      @callbacks << :around_before
      block.call
      @callbacks << :around_after
    end

    @record = @model.new(state: 'parked')
    @transition = StateMachines::Transition.new(@record, @machine, :ignite, :parked, :idling)
    @result = @transition.perform
  end

  def test_should_not_be_successful
    refute @result
  end

  def test_should_not_change_current_state
    assert_equal 'parked', @record.state
  end

  def test_should_not_run_action
    assert @record.new_record?
  end

  def test_should_not_run_further_callbacks
    assert_equal [:before_1], @callbacks
  end
end