Let's say I needed to stub a method so it returned the current time: MyClass.stub(:my_method).and_return(Time.now.utc)
The problem is, this stub returns the time at stub declaration, not when I run MyClass.new.my_method.
Is there any way to make a stub run when the method is called?
Pass in a block to and_return
that does the work you want done at runtime, like so...
describe MyClass do
it "runs at runtime" do
puts Time.now.utc
MyClass.stub(:my_method).and_return do
Time.now.utc
end
sleep 5.seconds
puts MyClass.my_method
end
end