Skip to content

Commit

Permalink
add a rough Spy::Instance#instead
Browse files Browse the repository at this point in the history
  • Loading branch information
jbodah committed Jul 1, 2016
1 parent 0880616 commit 7fe00f0
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 4 deletions.
5 changes: 5 additions & 0 deletions lib/spy/instance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def initialize(spied, original)
@around_procs = []
@call_history = []
@strategy = Strategy.factory_build(self)
@instead = nil
end

def name
Expand Down Expand Up @@ -67,6 +68,10 @@ def after(&block)
self
end

def instead(&block)
@instead = block
end

private

def extract_visibility
Expand Down
12 changes: 8 additions & 4 deletions lib/spy/instance/api/internal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,15 @@ def build_method_call(receiver, *args, &block)
end

def call_and_record(receiver, args, opts = {}, &block)
record = opts[:record] || build_method_call(receiver, *args, &block)
@call_history << record
if @instead
@instead.call build_method_call(receiver, *args, &block)
else
record = opts[:record] || build_method_call(receiver, *args, &block)
@call_history << record

result = call_original(receiver, *args, &block)
record.result = result
result = call_original(receiver, *args, &block)
record.result = result
end
end

def call_original(receiver, *args, &block)
Expand Down
74 changes: 74 additions & 0 deletions test/instead_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
require 'test_helper'

class InsteadTest < Minitest::Spec
describe 'Spy::Instance#instead' do
after do
Spy.restore(:all)
end

it 'will run the instead block and not the original call' do
o = Object.new
o.instance_eval do
def incr; total += 1; end
def total; @total ||= 0; end
def total=(n); @total = n; end
end

Spy.on(o, :incr).instead do |mc|
mc.receiver.total += 2
end

o.incr

assert_equal 2, o.total
end

it 'will return the value returned by the block' do
o = Object.new
o.instance_eval do
def name; 'josh'; end
end

Spy.on(o, :name).instead { 'penny' }

assert_equal 'penny', o.name
end

it 'plays nicely with Spy::Instance#when' do
o = Object.new
o.instance_eval do
def value=(n); @value = n; end
def value; @value; end
def name; 'josh'; end
end

Spy.on(o, :name).when { o.value == 0 }.instead { 'penny' }

o.value = 0
assert_equal 'penny', o.name

o.value = 1
assert_equal 'josh', o.name
end

it 'allows multiple whens and insteads' do
skip 'havent implemented multiple whens yet'

o = Object.new
o.instance_eval do
def value=(n); @value = n; end
def value; @value; end
def name; 'josh'; end
end

Spy.on(o, :name).when { o.value == 0 }.instead { 'penny' }
Spy.on(o, :name).when { o.value == 1 }.instead { 'lauren' }

o.value = 0
assert_equal 'penny', o.name

o.value = 1
assert_equal 'lauren', o.name
end
end
end

0 comments on commit 7fe00f0

Please sign in to comment.