File: machine_with_validations_test.rb

package info (click to toggle)
ruby-state-machines-activemodel 0.5.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 276 kB
  • sloc: ruby: 860; makefile: 5
file content (46 lines) | stat: -rw-r--r-- 1,293 bytes parent folder | download | duplicates (3)
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
46
require_relative 'test_helper'

class MachineWithValidationsTest < BaseTestCase
  def setup
    @model = new_model { include ActiveModel::Validations }
    @machine = StateMachines::Machine.new(@model, action: :save)
    @machine.state :parked

    @record = @model.new
  end

  def test_should_invalidate_using_errors
    I18n.backend = I18n::Backend::Simple.new if Object.const_defined?(:I18n)
    @record.state = 'parked'

    @machine.invalidate(@record, :state, :invalid_transition, [[:event, 'park']])
    assert_equal ['State cannot transition via "park"'], @record.errors.full_messages
  end

  def test_should_auto_prefix_custom_attributes_on_invalidation
    @machine.invalidate(@record, :event, :invalid)

    assert_equal ['State event is invalid'], @record.errors.full_messages
  end

  def test_should_clear_errors_on_reset
    @record.state = 'parked'
    @record.errors.add(:state, 'is invalid')

    @machine.reset(@record)
    assert_equal [], @record.errors.full_messages
  end

  def test_should_be_valid_if_state_is_known
    @record.state = 'parked'

    assert @record.valid?
  end

  def test_should_not_be_valid_if_state_is_unknown
    @record.state = 'invalid'

    assert !@record.valid?
    assert_equal ['State is invalid'], @record.errors.full_messages
  end
end