File: machine_with_column_state_attribute_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 (46 lines) | stat: -rw-r--r-- 1,167 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
46
# frozen_string_literal: true

require_relative 'test_helper'

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

    @record = @model.new
  end

  def test_should_not_override_the_column_reader
    @record[:state] = 'parked'
    assert_equal 'parked', @record.state
  end

  def test_should_not_override_the_column_writer
    @record.state = 'parked'
    assert_equal 'parked', @record[:state]
  end

  def test_should_have_an_attribute_predicate
    assert @record.respond_to?(:state?)
  end

  def test_should_test_for_existence_on_predicate_without_parameters
    assert @record.state?

    @record.state = nil
    refute @record.state?
  end

  def test_should_return_false_for_predicate_if_does_not_match_current_value
    refute @record.state?(:idling)
  end

  def test_should_return_true_for_predicate_if_matches_current_value
    assert @record.state?(:parked)
  end

  def test_should_raise_exception_for_predicate_if_invalid_state_specified
    assert_raises(IndexError) { @record.state?(:invalid) }
  end
end