Skip to content

Instantly share code, notes, and snippets.

@rubyist
Created August 8, 2009 16:46
Show Gist options
  • Save rubyist/164453 to your computer and use it in GitHub Desktop.
Save rubyist/164453 to your computer and use it in GitHub Desktop.
# spec -cfs user_spec.rb
require 'rubygems'
require 'aasm'
require 'active_record'
require 'fileutils'
FileUtils.rm_f('dbfile')
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => "dbfile")
ActiveRecord::Schema.create_table(:users) do |t|
t.string :name
t.string :state
end
class User < ActiveRecord::Base
include AASM
aasm_column :state
aasm_initial_state :pending
aasm_state :pending
aasm_state :current, :enter => :deliver
aasm_state :old, :enter => :end_now
aasm_event :activate do
transitions :from => :pending, :to => :current
end
aasm_event :expire do
transitions :from => :current, :to => :old
end
def deliver
end
def end_now
end
end
describe User do
it "should transition from pending to current with activate! event" do
my_obj = User.create!(:name => 'foo')
my_obj.activate!
my_obj.state.should eql('current')
end
it "should transition from current to old with expire! event" do
my_obj = User.create!(:name => 'bar')
my_obj.activate!
my_obj.state.should eql('current')
my_obj.expire!
my_obj.state.should eql('old')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment