Guest User

Untitled

a guest
Jul 4th, 2012
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. RSpec2: stub a method called by another method
  2. class Place < ActiveRecord::Base
  3. def choose_a_winner_for_attack (p_attack)
  4. puts "REAL choose_a_winner_for_attack"
  5. (rand() < p_attack)
  6. end
  7.  
  8. def attacks(attacked_place, attack_deployments)
  9. ….
  10. win = choose_a_winner_for_attack(p_attack)
  11. ….
  12. end
  13. end
  14.  
  15. place.stub!(:choose_a_winner_for_attack).and_return(true)
  16.  
  17. place.choose_a_winner_for_attack 0
  18.  
  19. place.attacks(…)
  20.  
  21. #Stub Place
  22. place = @user0.place
  23. place.stub!(:choose_a_winner_for_attack).and_return(true)
  24. puts "INSIDE SPEC #{f.object_id} #{f.choose_a_winner_for_attack 0}"
  25. place.attacks(other_place, deployments)
  26.  
  27. class A
  28. def foo
  29. "foo"
  30. end
  31.  
  32. def bar
  33. foo
  34. end
  35. end
  36.  
  37. describe A do
  38. it "stubs methods called from within other methods" do
  39. a = A.new
  40. a.stub(:foo).and_return("baz")
  41. a.foo.should == "baz" # passes
  42. a.bar.should == "baz" # passes
  43. end
  44. end
Advertisement
Add Comment
Please, Sign In to add comment