From: brabuhr@... Date: 2008-11-07T00:30:48+09:00 Subject: Re: Redefine while On Thu, Nov 6, 2008 at 10:00 AM, Emil Sandin wrote: > Hi, is there an easy way to redefine 'while'. I have a class that uses a > while loop. In my unit test it always evaluates to false (which is > correct). I only want the loop to run once, and the enter. Just like an > if. As others already noted, while is a keyword not a method. But depending on how your code is structured you might be able to mock the condition in your test: $ cat foo.rb class Foo attr_reader :i def initialize @i = 0 end def condition @i < 10 end def method while condition @i += 1 end end end $ cat foo_spec.rb require 'foo' describe Foo do it "should loop once" do f = Foo.new f.i.should == 0 f.method f.i.should == 10 end it "should loop once" do f = Foo.new f.should_receive(:condition).and_return(true, false) f.i.should == 0 f.method f.i.should == 1 end end $ spec foo_spec.rb .. Finished in 0.008461 seconds 2 examples, 0 failures