Guest User

Untitled

a guest
Mar 23rd, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. class A
  2. def a(arg)
  3. b(c(arg))
  4. end
  5.  
  6. def b(arg)
  7. do_expensive_thing(arg)
  8. end
  9.  
  10. def c(arg)
  11. do_other_expensive_thing(arg)
  12. end
  13. end
  14.  
  15. # OPTION A - stubbing methods of class being tested
  16. # benefit: don't have know exactly what `do_expensive_thing` does,
  17. # can just stub it entirely
  18. # cost: hard to refactor method being tested
  19.  
  20. test 'A#a' do
  21. inst = A.new
  22. arg = 1
  23. arg2 = 2
  24. arg3 = 3
  25. inst.expects(:c).with(arg).returns arg2
  26. inst.expects(:b).with(arg2).returns arg3
  27. assert_equal arg3, inst.a(arg)
  28. end
  29.  
  30. # OPTION 2 - Stub the methods being called lower in the stack
  31. # benefit: more flexibility with refactoring
  32. # cost: have to dig around for the exact methods that need to get stubbed
  33.  
  34. test 'A#a' do
  35. inst = A.new
  36. arg = 1
  37. arg2 = 2
  38. arg3 = 3
  39. ObscureClass.allows(:obscure_method).with(arg).returns arg2
  40. ObscureClass.allows(:other_obscure_method).returns arg3
  41. assert_equal arg3, inst.a(arg)
  42. end
Add Comment
Please, Sign In to add comment