[rspec-users] question about throw_symbol

Paul Mylchreest
Tue, 18 Aug 2009 19:16:33 -0000

Thanks David,

So, taking from your book, on page 212:

=begin
   def working_too_hard?
     catch :working_too_hard do
       weeks.each do |week|
         people.each do |person|
           throw :working_too_hard, true if person.hours_for(week) > 50
         end
       end
     end
   end

   To set an expectation that a symbol is thrown, we wrap up the code
in a proc
   and set the expectation on the proc:

   lambda {
     team.working_too_hard?
   }.should throw_symbol(:working_too_hard)
=end

I guess that is a typo, correct?

On 2009-Aug-18, at 14:34 , David Chelimsky wrote:

> On Tue, Aug 18, 2009 at 12:19 PM, Paul
> Mylchreest<paul.mylchreest at mac.com> wrote:
>> Given
>>  rspec 1.2.8
>>  ruby 1.8.6 (2008-08-11 patchlevel 287) [universal-darwin9.0]
>>
>> And the following spec:
>>
>>  def over_limit?(a)
>>    catch(:over_limit) do
>>      a.each do |x|
>>        throw(:over_limit) if x > 10
>>      end
>>    end
>>  end
>>
>>  describe "when over limit" do
>>    it "should throw symbol :over_limit" do
>>      lambda{
>>        over_limit?([0, 5, 11])
>>      }.should throw_symbol(:over_limit)
>>    end
>>  end
>>
>> One would expect this to pass, however this is the result:
>>
>>  when over limit
>>  - should throw symbol :over_limit (FAILED - 1)
>>
>>  1)
>>  ''when over limit should throw symbol :over_limit''
FAILED
>>  expected :over_limit but nothing was thrown
>>  ./spec/rspec_tests/rutabaga_spec.rb:125:
>>
>> When the catch is removed, it works.
>
> That''s correct. The catch in over_limit() catches the throw, so it
> does not bubble out of the method.
>
>>
>> Can anyone reproduce this behavior?
>>
>> thanks
>>
>> Paul Mylchreest
>> paul.mylchreest at mac.com
>>
>>
>>
>> _______________________________________________
>> rspec-users mailing list
>> rspec-users at rubyforge.org
>> http://rubyforge.org/mailman/listinfo/rspec-users
>>
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

Paul Mylchreest
paul.mylchreest at mac.com