Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 4th, 2012  |  syntax: None  |  size: 0.91 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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