[rspec-users] Continuing a "specify" block after failure

leenoori leenoori at quepica.com
Tue Feb 27 08:59:16 EST 2007


El 26/2/2007, a las 22:43, aslak hellesoy escribió:

> I have just checked in an example that illustrates this:
>
> http://rubyforge.org/viewvc/trunk/rspec/examples/dynamic_spec.rb? 
> revision=1537&root=rspec
>
> Aslak

I've discovered one minor problem with this approach; if you have a  
"context_setup" block inside your context then it will be run *after*  
the dynamic specify block. For example, look below where I've added a  
"context_setup" block to your example. Intuition might lead one to  
believe that it would be executed *before* the specs because it's  
labelled as a "setup" block and also because it appears physically  
before the "specify" block. But it actually runs after:

context "The month march" do
   context_setup do
     @start = 1
     @max = 10
   end
   (@start.. at max).each do |n|
     specify "The root of #{n} square should be #{n}" do
       Math.sqrt(n*n).should == n
     end
   end
end

No big deal, of course, but it can cause breakage (and does in the  
case of the contrived example). The workaround is to forget using  
"context_setup" and do the setup in the same scope as the following  
"specify" block:

context "The month march" do
   @start = 1
   @max = 10
   (@start.. at max).each do |n|
     specify "The root of #{n} square should be #{n}" do
       Math.sqrt(n*n).should == n
     end
   end
end

I suspect a robust fix would involve evaluating context_setup blocks  
as soon as they are encountered rather than deferring them until  
later. Should I file a bug report/enhance request for this?

Cheers :-)



More information about the rspec-users mailing list