Skip to content

Commit

Permalink
Added unit tests for the extensions provided by this railtie.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dave Sieh committed Nov 17, 2011
1 parent f408603 commit 14cd6b2
Show file tree
Hide file tree
Showing 6 changed files with 201 additions and 2 deletions.
1 change: 1 addition & 0 deletions Gemfile
Expand Up @@ -12,4 +12,5 @@ group :development do
gem "bundler", "~> 1.0.0"
gem "jeweler", "~> 1.6.4"
gem "rcov", ">= 0"
gem "sqlite3" # This is used for the spec tests (setting up activerecord models)
end
4 changes: 3 additions & 1 deletion Gemfile.lock
Expand Up @@ -88,7 +88,8 @@ GEM
sprockets (2.0.0)
hike (~> 1.2)
rack (~> 1.0)
tilt (!= 1.3.0, ~> 1.1)
tilt (~> 1.1, != 1.3.0)
sqlite3 (1.3.4)
thor (0.14.6)
tilt (1.3.3)
treetop (1.4.10)
Expand All @@ -106,3 +107,4 @@ DEPENDENCIES
rails (>= 3.1.0)
rcov
rspec (~> 2.3.0)
sqlite3
169 changes: 169 additions & 0 deletions spec/rails_extensions_spec.rb
@@ -0,0 +1,169 @@
require 'spec_helper'

module ClassMethod; end

class ClassMethod::MyModel < ActiveRecord::Base

def self.the_async_method(arg1, arg2)
WhatHappened.instance << "the static async method(#{arg1}, #{arg2})"
end

end


module InstanceMethod; end

class InstanceMethod::MyModel < ActiveRecord::Base

def the_async_method(arg1, arg2)
WhatHappened.instance << "the instance async method(#{arg1}, #{arg2})"
end

end

module AfterSave; end

# Setup the class to deal with a before_save callback
class AfterSave::MyModel < ActiveRecord::Base
ayl_after_save :handle_after_save

private

def handle_after_save
WhatHappened.instance << "handle after save"
end
end

module AfterUpdate; end

# Setup the class to deal with a before_save callback
class AfterUpdate::MyModel < ActiveRecord::Base
ayl_after_update :handle_after_update

private

def handle_after_update
WhatHappened.instance << "handle after update"
end
end

module AfterCreate; end

# Setup the class to deal with a before_save callback
class AfterCreate::MyModel < ActiveRecord::Base
ayl_after_create :handle_after_create

private

def handle_after_create
WhatHappened.instance << "handle after create"
end
end

describe "Rails Extensions" do

before(:each) do
# Set up a null logger
Ayl::Logger.instance.logger = Ayl::NullLogger.new

# Set up an in-memory database so that we can quickly do ActiveRecord
# tests.
ActiveRecord::Base.establish_connection(:adapter => 'sqlite3',
:database => ":memory:")

ActiveRecord::Schema.define do
create_table :my_models do |t|
t.column :name, :string
end
end

end

after(:each) do
WhatHappened.instance.clear
end

context "when handling lifecycle callbacks" do
it "the ayl_after_save handler should fire when the model is saved" do

model = AfterSave::MyModel.new(:name => 'spud')
model.save.should be_true

WhatHappened.instance.what_ran.should == [ 'handle after save' ]
WhatHappened.instance.clear

model.update_attribute(:name, 'joan')

WhatHappened.instance.what_ran.should == [ 'handle after save' ]
end

it "the ayl_after_update handler should fire when the model is updated" do

model = AfterUpdate::MyModel.new(:name => 'spud')
model.save.should be_true

WhatHappened.instance.what_ran.should be_nil

model.update_attribute(:name, 'joan')

WhatHappened.instance.what_ran.should == [ 'handle after update' ]
end

it "the ayl_after_create handler should fire when the model is created" do

model = AfterCreate::MyModel.new(:name => 'spud')
model.save.should be_true

WhatHappened.instance.what_ran.should == [ 'handle after create' ]
WhatHappened.instance.clear

model.update_attribute(:name, 'joan')

WhatHappened.instance.what_ran.should be_nil
end

end

context "when using the instance extensions" do

it "should represent the instance of a particular model using a 'find'" do
model = InstanceMethod::MyModel.create(:name => 'loud')

model.to_rrepr.should == "InstanceMethod::MyModel.find(#{model.id})"
end

it "should invoke the instance method asynchronously with no options" do
model = InstanceMethod::MyModel.create(:name => 'loud')

model.ayl_send(:the_async_method, "first", "second")

WhatHappened.instance.what_ran.should == [ "the instance async method(first, second)" ]
end

it "should invoke the instance method asynchronously with options" do
model = InstanceMethod::MyModel.create(:name => 'loud')

model.ayl_send_opts(:the_async_method, {}, "first", "second")

WhatHappened.instance.what_ran.should == [ "the instance async method(first, second)" ]
end

end

context "when using the class extensions" do

it "should invoke the static method asynchronously with no options" do
ClassMethod::MyModel.ayl_send(:the_async_method, "first", "second")

WhatHappened.instance.what_ran.should == [ "the static async method(first, second)" ]
end

it "should invoke the instance method asynchronously with options" do
ClassMethod::MyModel.ayl_send_opts(:the_async_method, {}, "first", "second")

WhatHappened.instance.what_ran.should == [ "the static async method(first, second)" ]
end

end

end
6 changes: 5 additions & 1 deletion spec/spec_helper.rb
@@ -1,9 +1,13 @@
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
$LOAD_PATH.unshift(File.dirname(__FILE__))
require 'rspec'
require 'rails'
require 'rails/all'
require 'ayl'
require 'ayl-rails'

# Run the initializers in the Railtie to get them to register
Ayl::Railtie.instance.run_initializers

# Requires supporting files with custom matchers and macros, etc,
# in ./support/ and its subdirectories.
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
Expand Down
16 changes: 16 additions & 0 deletions spec/support/null_logger.rb
@@ -0,0 +1,16 @@
module Ayl

#
# A logger to use to limit the amount of junk output while the test is running
#
class NullLogger

Ayl::Logger::LOG_METHODS.each do | method |
define_method(method) do |message|
# Do nothing
end
end

end

end
7 changes: 7 additions & 0 deletions spec/support/what_happened.rb
@@ -0,0 +1,7 @@
class WhatHappened
include Singleton
attr_accessor :what_ran
def <<(message) @what_ran ||= []; @what_ran << message end
def clear() @what_ran = nil end
end

0 comments on commit 14cd6b2

Please sign in to comment.