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.
Can anyone reproduce this behavior?
thanks
Paul Mylchreest
paul.mylchreest at mac.com
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 >
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
On 18 Aug 2009, at 20:16, Paul Mylchreest wrote:> So, taking from your book, on page 212:[...]> I guess that is a typo, correct?As an aside, I don''t understand this section of the book ("Expecting a Throw") at all. The original version of the loop already "short circuits" (with return) without using throw/catch. What does "as soon as working_too_hard == true" mean? And yes, it''s weird to be setting a throw_symbol expectation on a call to a method which is catching that throw. Cheers, -Tom
On Tue, Aug 18, 2009 at 2:46 PM, Tom Stuart<tom at experthuman.com> wrote:> On 18 Aug 2009, at 20:16, Paul Mylchreest wrote: >> >> So, taking from your book, on page 212: > > [...] >> >> I guess that is a typo, correct? > > As an aside, I don''t understand this section of the book ("Expecting a > Throw") at all. The original version of the loop already "short circuits" > (with return) without using throw/catch. What does "as soon as > working_too_hard == true" mean? And yes, it''s weird to be setting a > throw_symbol expectation on a call to a method which is catching that throw.Yes - this has been reported in the errata and has not yet been addressed. It will be before we get to treeware :)> > Cheers, > -Tom > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >