[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
Wed Jul 28 18:42:23 EDT 2010


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

  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


More information about the rspec-users mailing list