Skip to content

Instantly share code, notes, and snippets.

@maxp-edcast
Created March 14, 2018 23:13
Show Gist options
  • Save maxp-edcast/cd59610226408a293f0140b57a3682e5 to your computer and use it in GitHub Desktop.
Save maxp-edcast/cd59610226408a293f0140b57a3682e5 to your computer and use it in GitHub Desktop.
test
class A
def a(arg)
b(c(arg))
end
def b(arg)
do_expensive_thing(arg)
end
def c(arg)
do_other_expensive_thing(arg)
end
end
# OPTION A - stubbing methods of class being tested
# benefit: don't have know exactly what `do_expensive_thing` does,
# can just stub it entirely
# cost: hard to refactor method being tested
test 'A#a' do
inst = A.new
arg = 1
arg2 = 2
arg3 = 3
inst.expects(:c).with(arg).returns arg2
inst.expects(:b).with(arg2).returns arg3
assert_equal arg3, inst.a(arg)
end
# OPTION 2 - Stub the methods being called lower in the stack
# benefit: more flexibility with refactoring
# cost: have to dig around for the exact methods that need to get stubbed
test 'A#a' do
inst = A.new
arg = 1
arg2 = 2
arg3 = 3
ObscureClass.allows(:obscure_method).with(arg).returns arg2
ObscureClass.allows(:other_obscure_method).returns arg3
assert_equal arg3, inst.a(arg)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment