Permalink
Browse files

Adds a small syntax improvement

  • Loading branch information...
1 parent 4a81270 commit d636e077016b1f4c3be5e66de793780a8f029048 @casualjim committed Jan 24, 2010
View
@@ -1,4 +1,7 @@
-*** 12/01/2009 ***
+*** 24/01/2010 ***
+ * Adds ability set expectation on a plain object
+
+*** 12/01/201- ***
* Adds camel-cased method name support
*** 16/10/2009 ***
View
@@ -1,25 +1,25 @@
-require 'rubygems' unless defined?(Gem)
-$:.unshift File.dirname(__FILE__)
-require 'uuidtools'
-
-module Caricature
-#
-# module Interception
-#
-# end
-#
-end
-
-require 'caricature/core_ext'
-require 'caricature/version'
-require 'caricature/isolation'
-require 'caricature/clr' if defined? IRONRUBY_VERSION
-require 'caricature/bacon' if defined? Bacon
-require 'caricature/rspec' if defined? Spec
-
-# convenience method for creating an isolation. aliased as mock and stub for less surprises
-def isolate(subject, recorder = Caricature::MethodCallRecorder.new, expectations = Caricature::Expectations.new)
- Caricature::Isolation.for(subject, recorder, expectations)
-end
-alias :mock :isolate
-alias :stub :isolate
+require 'rubygems' unless defined?(Gem)
+$:.unshift File.dirname(__FILE__)
+require 'uuidtools'
+
+module Caricature
+#
+# module Interception
+#
+# end
+#
+end
+
+require 'caricature/core_ext'
+require 'caricature/version'
+require 'caricature/isolation'
+require 'caricature/clr' if defined? IRONRUBY_VERSION
+require 'caricature/bacon' if defined? Bacon
+require 'caricature/rspec' if defined? Spec
+
+# convenience method for creating an isolation. aliased as mock and stub for less surprises
+def isolation_for(subject, recorder = Caricature::MethodCallRecorder.new, expectations = Caricature::Expectations.new)
+ Caricature::Isolation.for(subject, recorder, expectations)
+end
+alias :mock_for :isolation_for
+alias :stub_for :isolation_for
@@ -12,4 +12,14 @@ def clr_type?
self.ancestors.reject {|mod| mod == Object }.any? { |mod| !mod.to_clr_type.nil? }
end
+ def isolate(name=nil, recorder = Caricature::MethodCallRecorder.new, expectations = Caricature::Expectations.new, &block)
+ iso = Caricature::Isolation.for(self, recorder, expectations)
+ return iso unless name
+ iso.when_class_receives(name, &block)
+ iso
+ end
+ alias_method :when_receiving, :isolate
+ alias_method :mock, :isolate
+ alias_method :stub, :isolate
+
end
@@ -15,5 +15,15 @@ def to_clr_type
def define_cmethod(name, &blk)
(class << self; self; end).instance_eval { define_method name, &blk }
end
-
+
+ public
+ def isolate(name=nil, recorder = Caricature::MethodCallRecorder.new, expectations = Caricature::Expectations.new, &block)
+ iso = Caricature::Isolation.for(self, recorder, expectations)
+ return iso unless name
+ iso.when_receiving(name, &block)
+ iso
+ end
+ alias_method :when_receiving, :isolate
+ alias_method :mock, :isolate
+ alias_method :stub, :isolate
end
@@ -113,7 +113,7 @@ def for(subject, recorder = MethodCallRecorder.new, expectations = Expectations.
isolator = RubyIsolator.for context
isolation = new(isolator, context)
- isolator.isolation
+ isolation
end
end
@@ -70,7 +70,7 @@
before do
@ninja = ClrModels::Ninja.new
- @weapon = isolate ClrModels::IWeapon
+ @weapon = isolation_for ClrModels::IWeapon
end
@@ -81,7 +81,7 @@
describe "when isolating CLR interfaces" do
before do
@ninja = ClrModels::Ninja.new
- @weapon = isolate ClrModels::IWeapon
+ @weapon = isolation_for ClrModels::IWeapon
end
it "should work without expectations" do
@@ -151,7 +151,7 @@
describe "plain vanilla CLR classes" do
before do
@weapon = ClrModels::Sword.new
- @ninja = isolate ClrModels::Ninja
+ @ninja = isolation_for ClrModels::Ninja
end
it "should work without expectations" do
@@ -223,7 +223,7 @@
result = @weapon.attack @ninja
result.should.equal 5
- @ninja.should.not.have_received?(:survive_attack_with) {|v| v.with(isolate(ClrModels::IWeapon)) }
+ @ninja.should.not.have_received?(:survive_attack_with) {|v| v.with(isolation_for(ClrModels::IWeapon)) }
end
end
@@ -76,7 +76,7 @@ class DoNothingEventArgs < System::EventArgs
describe "for CLR interfaces" do
before do
- @proxy = isolate ClrModels::IExplodingWarrior
+ @proxy = isolation_for ClrModels::IExplodingWarrior
@subscriber = ClrModels::ExposedChangedSubscriber.new(@proxy)
end
@@ -87,7 +87,7 @@ class DoNothingEventArgs < System::EventArgs
describe "for CLR classes" do
before do
- @proxy = isolate ClrModels::ExposingWarrior
+ @proxy = isolation_for ClrModels::ExposingWarrior
@subscriber = ClrModels::ExposedChangedSubscriber.new(@proxy)
end
@@ -0,0 +1,33 @@
+require File.dirname(__FILE__) + "/../spec_helper"
+
+describe "syntax improvements" do
+
+ describe "creation from a class method" do
+
+ it "should allow creating an isolation" do
+ soldier = Soldier.isolate
+ soldier.should.not.be.nil
+ end
+
+ it "should allow setting an expectation" do
+ soldier = SoldierWithClassMembers.isolate(:class_name){ |exp| exp.return("overridden") }
+ soldier.class.class_name.should == "overridden"
+ end
+
+ end
+
+
+ describe "creation from an instance method" do
+
+ it "should allow creating an isolation" do
+ soldier = Soldier.new.isolate
+ soldier.should.not.be.nil
+ end
+
+ it "should allow setting an expectation" do
+ soldier = Soldier.new.isolate(:name){ |exp| exp.return("overridden") }
+ soldier.name.should == "overridden"
+ end
+
+ end
+end
@@ -5,7 +5,7 @@
context "for CLR interfaces" do
before do
- @proxy = isolate ClrModels::IExplodingWarrior
+ @proxy = isolation_for ClrModels::IExplodingWarrior
end
it "should not raise an error when subcribing to an event" do

0 comments on commit d636e07

Please sign in to comment.