Permalink
Browse files

Started working on message expectations

  • Loading branch information...
1 parent 9009c33 commit f5d887a4cb7fd16c7f0b4cd573759dcf3dae3eb2 @txus committed Nov 4, 2010
Showing with 46 additions and 0 deletions.
  1. +1 −0 lib/stendhal/mocks.rb
  2. +25 −0 lib/stendhal/mocks/spyable.rb
  3. +20 −0 spec/stendhal/spyable_spec.rb
View
@@ -1,4 +1,5 @@
require 'stendhal/mocks/test_double'
+require 'stendhal/mocks/spyable'
module Stendhal
module Mocks
@@ -0,0 +1,25 @@
+module Stendhal
+ module Mocks
+ module Spyable
+ # def included(base)
+ # base.send(:include, InstanceMethods)
+ # end
+
+ # module InstanceMethods
+ def spy(method)
+ metaclass = (class << self;self;end)
+ metaclass.send(:alias_method, :"__original_#{method}", method.to_sym)
+ metaclass.send(:undef_method, method.to_sym)
+ metaclass.class_eval <<EOT
+ def #{method}(*args, &block)
+ puts "spy says... called #{method}!"
+ @__verifier.add_call(:#{method},*args,&block) if nil
+ __original_#{method}(*args,&block)
+ end
+EOT
+ end
+ # end
+
+ end
+ end
+end
@@ -0,0 +1,20 @@
+require 'spec_helper'
+
+class MyClass < Array
+ include Stendhal::Mocks::Spyable
+end
+
+module Stendhal
+ module Mocks
+ describe "a Spyable class" do
+
+ subject { MyClass.new([1,2,3]) }
+
+ it 'acts as a proxy for the target' do
+ subject.spy(:length)
+ subject.length
+ end
+
+ end
+ end
+end

0 comments on commit f5d887a

Please sign in to comment.