Skip to content

Instantly share code, notes, and snippets.

@awendt
Created January 14, 2015 16:06
Show Gist options
  • Save awendt/cfa4ec3ba474b2c4825f to your computer and use it in GitHub Desktop.
Save awendt/cfa4ec3ba474b2c4825f to your computer and use it in GitHub Desktop.
AASM test case
gem 'activerecord', '3.2.19'
require 'active_record'
require 'minitest/autorun'
require 'logger'
gem 'aasm', '4.0.8'
require 'aasm'
# 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 :users do |t|
end
create_table :verifications do |t|
t.integer :user_id
t.string :state
end
end
class User < ActiveRecord::Base
has_one :verification
def verification_with_find_or_create_and_initialize(*args)
verification_without_find_or_create_and_initialize(*args) || create_verification
end
alias_method_chain :verification, :find_or_create_and_initialize
end
class Verification < ActiveRecord::Base
belongs_to :user
include AASM
aasm column: 'state', whiny_transitions: false do
state :initialized, initial: true, after_enter: [:enqueue_something]
state :confirmed
event :confirm do
transitions from: :initialized, to: :confirmed
end
end
def enqueue_something
persisted? or raise 'Not persisted'
end
end
class BugTest < Minitest::Test
def test_after_enter
user = User.create
user.create_verification
assert user.verification.persisted?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment