25 Jan 2010, 19:35
Generic-user-small

Robert Winter (5 posts)

Hi,
The underlying theme of this post is that I’d like to see a little more in the book on the ‘architecture’ of how Rspec works, so that debugging weird scope issues or values becomes a little more comprehensible.

I’ve written a bunch of rspec tests in the following style. Is this a good style? Am I right in guessing that each Describe and It invocation binds up the current environment as well as the block? I’ve run into what I thought were scoping issues too (more below).

require '../spec_helper'

class Test_scope
  attr_accessor :value
  def initialize(a)
    @value = a
  end
end

describe "overall tests" do
  def func_xy(m,n)
    Test_scope.new([n,m])
  end
  def predict_value_inside(p,q)
    [q,p]
  end
  (1..10).each do |x|
    (1..10).each do |y|
      describe "Test_scope #{x} #{y}" do 
        before(:each) do
          @test_scope = func_xy(x, y)
        end

        it "should describe #{x} #{y}" do
          @test_scope.value.should == predict_value_inside(x, y)
        end
      end
    end
  end
end

I swear I ran into an issue where ‘predict_value_inside’ had to be defined outside the ‘describe’ blocks, but of course it’s a few weeks later and this simple example isn’t demonstrating the issue.

Are there gotchas with scoping and Ruby 1.8.7?

26 Jan 2010, 13:37
Dchelimsky_pragsmall

David Chelimsky (206 posts)

You may have run into a problem at one point with Ruby 1.9, but this works fine in 1.8 (and currently in 1.9 as well).

08 Feb 2010, 23:45
Generic-user-small

Robert Winter (5 posts)

Thanks for the reply. I am not using 1.9, so I’ll just have to post a more concrete example if I can ever recreate the scope issue.

I’ve realized I’m also confused as to when to use an instance variable or not. I’m debugging a test structured exactly like my above sample that has 346 examples and 173 failures. I’m dumping debugging information using puts statements inside my ‘it’ block. Of course that appears in the ‘upper half’ of the rspec output, separated from the 173 error messages. I tried to put a counter variable in so that I can match upper and lower halves, but neither instance variable @x or local variable x is working the way I’d expect. Because I’m using a custom matcher shared among several spec files I’m looking for an alternative to modifying my custom matcher to only work with this one spec file just for debugging. Also, does my custom matcher have access to the instance variables I’ve created? How does communication work between spec file and custom matchers?

  You must be logged in to comment