Doug Livesey
2008-Jun-05 09:46 UTC
[rspec-users] Using and_yield to pass multiple or no iterations
Hi, imagine there''s a class called Egg which has the following method (which calls another method): <CODE> def do_thing has_iterated = false self.each_row do |row| has_iterated = true unless has_iterated end has_iterated end </CODE> Stupid code, I know. I have two questions with it. The first is, would it be possible to set it up to test the case when each_row operates on an empty Array? Sort of like this: <SPEC> it "should return false if it does not iterate over anything" do @egg.stub!( each_row ).and_yield( nil ) # I know this doesn''t work @egg.do_thing.should be_false end it "should return true if it does iterate over something" do @egg.stub!( each_row ).and_yield( :value ) @egg.do_thing.should be_true end </SPEC> Secondly, is this the best (correct) way to pass multiple values to iterate over? <SPEC> it "should be doing something else, too" @egg.stub!( :each_row ). and_yield( :first_value ). and_yield( :second_value ). and_yield( :third_value ) @egg.do_thing.should be_true end </SPEC> As you can see, my understanding of and_yield() is *very* imperfect, so any & all pointers are very gratefully received. Cheers, Doug. -- Posted via http://www.ruby-forum.com/.
John D. Hume
2008-Jun-05 16:30 UTC
[rspec-users] Using and_yield to pass multiple or no iterations
On Thu, Jun 5, 2008 at 5:46 AM, Doug Livesey <lists at ruby-forum.com> wrote:> I have two questions with it. The first is, would it be possible to set > it up to test the case when each_row operates on an empty Array?If there were no rows, each_row wouldn''t yield at all, so you should just be able to do @o.stub! :each_row (with no and_yield). Note that Ruby is happy to let you associate a block with any method call, whether it''s expected or not. -hume.