convert specs to tests

This commit is contained in:
rick 2008-06-27 23:29:03 -07:00
parent 10c581a6de
commit 7fc628e3fc
4 changed files with 146 additions and 137 deletions

@ -1,120 +0,0 @@
require File.join(File.dirname(__FILE__), 'spec_helper')
class ObservedModel < ActiveModel::Base
class Observer
end
end
class FooObserver < ActiveModel::Observer
class << self
public :new
end
attr_accessor :stub
def on_spec(record)
stub.event_with(record) if stub
end
end
class Foo < ActiveModel::Base
end
module ActiveModel
describe Observing do
before do
ObservedModel.observers.clear
end
it "initializes model with no cached observers" do
ObservedModel.observers.should be_empty
end
it "stores cached observers in an array" do
ObservedModel.observers << :foo
ObservedModel.observers.should include(:foo)
end
it "flattens array of assigned cached observers" do
ObservedModel.observers = [[:foo], :bar]
ObservedModel.observers.should include(:foo)
ObservedModel.observers.should include(:bar)
end
it "instantiates observer names passed as strings" do
ObservedModel.observers << 'foo_observer'
FooObserver.should_receive(:instance)
ObservedModel.instantiate_observers
end
it "instantiates observer names passed as symbols" do
ObservedModel.observers << :foo_observer
FooObserver.should_receive(:instance)
ObservedModel.instantiate_observers
end
it "instantiates observer classes" do
ObservedModel.observers << ObservedModel::Observer
ObservedModel::Observer.should_receive(:instance)
ObservedModel.instantiate_observers
end
it "should pass observers to subclasses" do
FooObserver.instance
bar = Class.new(Foo)
bar.count_observers.should == 1
end
end
describe Observer do
before do
ObservedModel.observers = :foo_observer
FooObserver.models = nil
end
it "guesses implicit observable model name" do
FooObserver.observed_class_name.should == 'Foo'
end
it "tracks implicit observable models" do
instance = FooObserver.new
instance.send(:observed_classes).should include(Foo)
instance.send(:observed_classes).should_not include(ObservedModel)
end
it "tracks explicit observed model class" do
FooObserver.new.send(:observed_classes).should_not include(ObservedModel)
FooObserver.observe ObservedModel
instance = FooObserver.new
instance.send(:observed_classes).should include(ObservedModel)
end
it "tracks explicit observed model as string" do
FooObserver.new.send(:observed_classes).should_not include(ObservedModel)
FooObserver.observe 'observed_model'
instance = FooObserver.new
instance.send(:observed_classes).should include(ObservedModel)
end
it "tracks explicit observed model as symbol" do
FooObserver.new.send(:observed_classes).should_not include(ObservedModel)
FooObserver.observe :observed_model
instance = FooObserver.new
instance.send(:observed_classes).should include(ObservedModel)
end
it "calls existing observer event" do
foo = Foo.new
FooObserver.instance.stub = stub!(:stub)
FooObserver.instance.stub.should_receive(:event_with).with(foo)
Foo.send(:changed)
Foo.send(:notify_observers, :on_spec, foo)
end
it "skips nonexistent observer event" do
foo = Foo.new
Foo.send(:changed)
Foo.send(:notify_observers, :whatever, foo)
end
end
end

@ -1,17 +0,0 @@
ENV['LOG_NAME'] = 'spec'
$LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'vendor', 'rspec', 'lib')
$LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
require 'active_model'
begin
require 'spec'
rescue LoadError
require 'rubygems'
require 'spec'
end
begin
require 'ruby-debug'
Debugger.start
rescue LoadError
# you do not know the ways of ruby-debug yet, what a shame
end

@ -0,0 +1,123 @@
require File.join(File.dirname(__FILE__), 'test_helper')
class ObservedModel < ActiveModel::Base
class Observer
end
end
class FooObserver < ActiveModel::Observer
class << self
public :new
end
attr_accessor :stub
def on_spec(record)
stub.event_with(record) if stub
end
end
class Foo < ActiveModel::Base
end
class ObservingTest < ActiveSupport::TestCase
def setup
ObservedModel.observers.clear
end
test "initializes model with no cached observers" do
assert ObservedModel.observers.empty?, "Not empty: #{ObservedModel.observers.inspect}"
end
test "stores cached observers in an array" do
ObservedModel.observers << :foo
assert ObservedModel.observers.include?(:foo), ":foo not in #{ObservedModel.observers.inspect}"
end
test "flattens array of assigned cached observers" do
ObservedModel.observers = [[:foo], :bar]
assert ObservedModel.observers.include?(:foo), ":foo not in #{ObservedModel.observers.inspect}"
assert ObservedModel.observers.include?(:bar), ":bar not in #{ObservedModel.observers.inspect}"
end
uses_mocha "observer instantiation" do
test "instantiates observer names passed as strings" do
ObservedModel.observers << 'foo_observer'
FooObserver.expects(:instance)
ObservedModel.instantiate_observers
end
test "instantiates observer names passed as symbols" do
ObservedModel.observers << :foo_observer
FooObserver.expects(:instance)
ObservedModel.instantiate_observers
end
test "instantiates observer classes" do
ObservedModel.observers << ObservedModel::Observer
ObservedModel::Observer.expects(:instance)
ObservedModel.instantiate_observers
end
end
test "passes observers to subclasses" do
FooObserver.instance
bar = Class.new(Foo)
assert_equal Foo.count_observers, bar.count_observers
end
end
class ObserverTest < ActiveSupport::TestCase
def setup
ObservedModel.observers = :foo_observer
FooObserver.models = nil
end
test "guesses implicit observable model name" do
assert_equal 'Foo', FooObserver.observed_class_name
end
test "tracks implicit observable models" do
instance = FooObserver.new
assert instance.send(:observed_classes).include?(Foo), "Foo not in #{instance.send(:observed_classes).inspect}"
assert !instance.send(:observed_classes).include?(ObservedModel), "ObservedModel in #{instance.send(:observed_classes).inspect}"
end
test "tracks explicit observed model class" do
old_instance = FooObserver.new
assert !old_instance.send(:observed_classes).include?(ObservedModel), "ObservedModel in #{old_instance.send(:observed_classes).inspect}"
FooObserver.observe ObservedModel
instance = FooObserver.new
assert instance.send(:observed_classes).include?(ObservedModel), "ObservedModel not in #{instance.send(:observed_classes).inspect}"
end
test "tracks explicit observed model as string" do
old_instance = FooObserver.new
assert !old_instance.send(:observed_classes).include?(ObservedModel), "ObservedModel in #{old_instance.send(:observed_classes).inspect}"
FooObserver.observe 'observed_model'
instance = FooObserver.new
assert instance.send(:observed_classes).include?(ObservedModel), "ObservedModel not in #{instance.send(:observed_classes).inspect}"
end
test "tracks explicit observed model as symbol" do
old_instance = FooObserver.new
assert !old_instance.send(:observed_classes).include?(ObservedModel), "ObservedModel in #{old_instance.send(:observed_classes).inspect}"
FooObserver.observe :observed_model
instance = FooObserver.new
assert instance.send(:observed_classes).include?(ObservedModel), "ObservedModel not in #{instance.send(:observed_classes).inspect}"
end
test "calls existing observer event" do
foo = Foo.new
FooObserver.instance.stub = stub
FooObserver.instance.stub.expects(:event_with).with(foo)
Foo.send(:changed)
Foo.send(:notify_observers, :on_spec, foo)
end
test "skips nonexistent observer event" do
foo = Foo.new
Foo.send(:changed)
Foo.send(:notify_observers, :whatever, foo)
end
end

@ -0,0 +1,23 @@
$:.unshift "#{File.dirname(__FILE__)}/../lib"
$:.unshift File.dirname(__FILE__)
require 'test/unit'
require 'active_model'
require 'active_support/callbacks' # needed by ActiveSupport::TestCase
require 'active_support/test_case'
def uses_gem(gem_name, test_name, version = '> 0')
require 'rubygems'
gem gem_name.to_s, version
require gem_name.to_s
yield
rescue LoadError
$stderr.puts "Skipping #{test_name} tests. `gem install #{gem_name}` and try again."
end
# Wrap tests that use Mocha and skip if unavailable.
unless defined? uses_mocha
def uses_mocha(test_name, &block)
uses_gem('mocha', test_name, '>= 0.5.5', &block)
end
end