Skip to content

Instantly share code, notes, and snippets.

@RaVbaker
Forked from Tuhaj/initial_state_bug.rb
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RaVbaker/5d076449247e5e0fc453 to your computer and use it in GitHub Desktop.
Save RaVbaker/5d076449247e5e0fc453 to your computer and use it in GitHub Desktop.
Corrected version of initial repo by @Tuhaj
unless File.exist?('Gemfile')
File.write('Gemfile', <<-GEMFILE)
source 'https://rubygems.org'
gem 'activerecord', '4.2.1'
gem 'state_machines'
gem 'state_machines-activerecord'
gem 'sqlite3'
gem 'minitest'
GEMFILE
system 'bundle'
end
require 'bundler'
Bundler.setup(:default)
# Activate the gem you are reporting the issue against.
require 'active_record'
require 'minitest/autorun'
require 'logger'
require 'state_machines'
require 'state_machines-activerecord'
# Ensure backward compatibility with Minitest 4
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)
# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :lights, force: true do |t|
t.string :state
end
end
class Light < ActiveRecord::Base
state_machine initial: :green do
state :green
state :yellow
state :red
end
end
class BugTest < Minitest::Test
def test_initial_state
light = Light.new
assert_equal 'green', light.state
light.save!
light = Light.find(light.id)
refute_equal nil, light.state
assert_equal 'green', light.state
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment