
Untitled
By: a guest on
Jul 4th, 2012 | syntax:
None | size: 0.91 KB | hits: 14 | expires: Never
RSpec2: stub a method called by another method
class Place < ActiveRecord::Base
def choose_a_winner_for_attack (p_attack)
puts "REAL choose_a_winner_for_attack"
(rand() < p_attack)
end
def attacks(attacked_place, attack_deployments)
….
win = choose_a_winner_for_attack(p_attack)
….
end
end
place.stub!(:choose_a_winner_for_attack).and_return(true)
place.choose_a_winner_for_attack 0
place.attacks(…)
#Stub Place
place = @user0.place
place.stub!(:choose_a_winner_for_attack).and_return(true)
puts "INSIDE SPEC #{f.object_id} #{f.choose_a_winner_for_attack 0}"
place.attacks(other_place, deployments)
class A
def foo
"foo"
end
def bar
foo
end
end
describe A do
it "stubs methods called from within other methods" do
a = A.new
a.stub(:foo).and_return("baz")
a.foo.should == "baz" # passes
a.bar.should == "baz" # passes
end
end