On Thu, May 29, 2008 at 9:25 AM, zuo peng <[EMAIL PROTECTED]> wrote:

> Hi,
>
> I've got trouble when describe infinite loop.
>
> code snippet:
>
>  def start_loop
>    while true

Not a reply to your specific question, but hopefully even better.

I stumbled into similar situation a while ago, when I was developing a
daemon process using BDD.
When writing the test, I came up naturally with the following kind of structure:

def start_loop
   while loop_condition?
      do_the_magic
   end
end

the idea is to separate looping logic and the operations done inside.
The whole method start_loop was very easy to test now:

MyDaemon.stub!(:loop_condition?).and_return(true, false) # I didn't
use RSpec at the time, so this might not work
...
outcome.should be_something_expected

The trick is in stubbing the class/module method loop_condition? so
that it returns true the first time and false the second time,
thus looping only once. In the actual implementation loop_condition?
was implemented as follows:

class MyDaemon
  def loop_condition?; true; end
  ...
end

thus making the while loop indefinite.

Any smart developer could have come up with similar structure without
using TDD/BDD. However, that particular solution came to me
naturally exactly because of the way BDD works: it forces you -- in a
tender way -- to write code that is easy to test automatically.

-- 
"One day, when he was naughty, Mr Bunnsy looked over the hedge into
Farmer Fred's field and it was full of fresh green lettuces. Mr
Bunnsy, however, was not full of lettuces. This did not seem fair."
 -- Terry Pratchett, Mr. Bunnsy Has An Adventure
_______________________________________________
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Reply via email to