[rspec-users] how to write spec for infinite loop?

Scott Taylor mailing_lists at railsnewbie.com
Thu May 29 03:31:06 EDT 2008

On May 29, 2008, at 2:25 AM, zuo peng wrote:

> Hi,
> I've got trouble when describe infinite loop.
> code snippet:
>  def start_loop
>    while true
>      data = self.server.handle_client
>      if data
>        self.manager.dispatch(data)
>      end
>    end
>  end
> without the loop, it is easy to test the logic.
> but how can I describe it to tell the developer ( me :-) ) that there
> should be an infinite loop inside.

Here's a tip which Aslak gave me several months ago, and I find myself  
repeating it in many different contexts on this mailing list:

One way is with dependency injection:

def start_loop(looping_infinitely = true)
   while looping_infinitely

In your spec, you simply pass false (but production code can call the  
method as if there is no option).


More information about the rspec-users mailing list