Sha256: d11f013ef95d5fb3c3a49538e1717f9e43da0d0d3bbd76a7796289dd9ed29b33

Contents?: true

Size: 1.43 KB

Versions: 1

Compression:

Stored size: 1.43 KB

Contents

require "helper"
require 'active_support/core_ext/module/aliasing'

ActiveRecord::Base.establish_connection(:adapter  => "sqlite3", :database => ":memory:")

class CreateTrafficLights < ActiveRecord::Migration
  def self.up
    create_table(:traffic_lights) do |t|
      t.string :state
      t.string :name
    end
  end
end

class TrafficLight < ActiveRecord::Base
  include ActiveRecord::Transitions

  state_machine do
    state :off

    state :red
    state :green
    state :yellow

    event :red_on do
      transitions :to => :red, :from => [:yellow]
    end

    event :green_on do
      transitions :to => :green, :from => [:red]
    end

    event :yellow_on do
      transitions :to => :yellow, :from => [:green]
    end

    event :reset do
      transitions :to => :red, :from => [:off]
    end
  end
end

class TestScopes < Test::Unit::TestCase
  def setup
    ActiveRecord::Base.establish_connection(:adapter  => "sqlite3", :database => ":memory:")
    ActiveRecord::Migration.verbose = false
    CreateTrafficLights.migrate(:up)

    @light = TrafficLight.create!
  end

  test "scope returns correct object" do
    assert TrafficLight.respond_to? :off
    assert_equal TrafficLight.off.first, @light
    assert TrafficLight.red.empty?
  end

  test "scopes exist" do
    assert TrafficLight.respond_to? :off
    assert TrafficLight.respond_to? :red
    assert TrafficLight.respond_to? :green
    assert TrafficLight.respond_to? :yellow
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
transitions-0.0.11 test/test_scopes.rb