File: test_helper.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 (56 lines) | stat: -rw-r--r-- 1,325 bytes parent folder | download | duplicates (2)
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
47
48
49
50
51
52
53
54
55
56
begin
  require 'pry-byebug'
rescue LoadError
end

require 'state_machines-activemodel'
require 'minitest/autorun'
require 'minitest/reporters'
require 'active_support/all'
Minitest::Reporters.use! [Minitest::Reporters::ProgressReporter.new]
I18n.enforce_available_locales = true

class BaseTestCase < MiniTest::Test
  protected
  # Creates a new ActiveModel model (and the associated table)
  def new_model(&block)
    # Simple ActiveModel superclass
    parent = Class.new do
      def self.model_attribute(name)
        define_method(name) { instance_variable_defined?("@#{name}") ? instance_variable_get("@#{name}") : nil }
        define_method("#{name}=") do |value|
          send("#{name}_will_change!") if self.class <= ActiveModel::Dirty && value != send(name)
          instance_variable_set("@#{name}", value)
        end
      end

      def self.create
        object = new
        object.save
        object
      end

      def initialize(attrs = {})
        attrs.each { |attr, value| send("#{attr}=", value) }
      end

      def attributes
        @attributes ||= {}
      end

      def save
        true
      end
    end

    model = Class.new(parent) do
      def self.name
        'Foo'
      end

      model_attribute :state
    end
    model.class_eval(&block) if block_given?
    model
  end
end