[rspec-users] rspec 1 - nested examples (in an each block) use the final block variable 4 times instead of each of the 4 once

nruth nick.rutherford at gmail.com
Fri Jul 30 17:00:46 EDT 2010


Hi David

I think this was a case of idiot-end-user (or developer) on my part,
sorry about that.

I've produced a simple example here (and figured out what I was doing
wrong).

http://gist.github.com/501296

I don't think anything needs to change, though a wrapper function
(each + an inner describe) might help flag it as a possible pitfall.

Thanks,
Nick

On Jul 29, 1:22 am, David Chelimsky <dchelim... at gmail.com> wrote:
> On Jul 28, 2010, at 5:42 PM, nruth wrote:
>
>
>
>
>
> > Please correct me if this is fixed in Rspec 2, but in Rspec 1 I have
> > hit upon the following problem (at least thrice, this time it costing
> > me several hours), code is the best example:
>
> > I have a practice examination system where each record is one of
> > several "subtests" (think tagging rather than subclassing) which is
> > part of a constant string array. I want to test each's behaviour
> > indepently in my specs (since an earlier implementation used STI
> > rather than tagging, and I want to test each kind of test follows the
> > spec)
>
> > My specs use this approach in parts:
>
> > describe "#add_question_set_of_type" do
> >  MyModule::SUBTESTS.each do |subtest|
>
> >    describe %Q("#{subtest}") do
> >    … specs using subtest
> >  end
> > end
>
> > where SUBTESTS is a constant array of strings.
>
> > the problem is doing this breaks any blocks that look like
>
> Where are these blocks, in the outer or inner example group? Please provide a complete example, including everything I need to run and see the output you're seeing.
>
> Thx
>
>
>
>
>
>
>
> >  let(:subtest) {MyModule.subtest_to_sym(subtest)}
>
> > or
>
> >  before(:each) do
> >    @subtest = MyModule.subtest_to_sym(subtest)
> >  end
>
> > Instead I have to avoid the @ or let examples, and use the more
> > explicit (verbose):
>
> >  MyModule.subtest_to_sym(subtest)
>
> > using the let or before @ approaches both fail.
>
> > Using puts within the code (for debugging purposes) I found that the
> > specs were only being exposed to the final value of the array, while I
> > expected (and have seen, or at least assumed from past passes) that
> > all 4 (or so) of the strings were being used to create unique methods
> > on the example group (one per iteration).
>
> > Is this something that should be:
>
> > a. avoided, because it's crazy, and written differently
> > b, documented
> > c. investigated further
>
> > I can provide a more complete example if helpful?
>
> > Thanks,
> > Nick
> > _______________________________________________
> > rspec-users mailing list
> > rspec-us... at rubyforge.org
> >http://rubyforge.org/mailman/listinfo/rspec-users
>
> _______________________________________________
> rspec-users mailing list
> rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users


More information about the rspec-users mailing list