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

David Chelimsky dchelimsky at gmail.com
Thu May 29 03:33:57 EDT 2008


On May 29, 2008, at 12:31 AM, Scott Taylor wrote:

>
> 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)

:)

I think calling this dependency injection is a bit of a stretch. I  
agree with the approach of having a logical default that you can  
override in the example, but what exactly is the dependency on? true?

Regardless of its name, this is a very good solution to the problem at  
hand.

>
>  while looping_infinitely
> 	...
>  end
> end
>
> In your spec, you simply pass false (but production code can call  
> the method as if there is no option).
>
> Scott
>
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users



More information about the rspec-users mailing list