Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
Add Comment
Please, Sign In to add comment