File: machine_with_state_driven_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 (31 lines) | stat: -rw-r--r-- 874 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
require_relative 'test_helper'

class MachineWithStateDrivenValidationsTest < BaseTestCase
  def setup
    @model = new_model do
      include ActiveModel::Validations
      attr_accessor :seatbelt
    end

    @machine = StateMachines::Machine.new(@model)
    @machine.state :first_gear, :second_gear do
      validates_presence_of :seatbelt
    end
    @machine.other_states :parked
  end

  def test_should_be_valid_if_validation_fails_outside_state_scope
    record = @model.new(state: 'parked', seatbelt: nil)
    assert record.valid?
  end

  def test_should_be_invalid_if_validation_fails_within_state_scope
    record = @model.new(state: 'first_gear', seatbelt: nil)
    assert !record.valid?
  end

  def test_should_be_valid_if_validation_succeeds_within_state_scope
    record = @model.new(state: 'second_gear', seatbelt: true)
    assert record.valid?
  end
end