[rspec-users] RSpec framework question

Matt Wynne matt at mattwynne.net
Tue Mar 2 18:16:26 EST 2010


On 2 Mar 2010, at 21:05, Adam R wrote:

>
> An update for this issue.  This script works, but only if I  
> explicitly define
> the array that I'm iterating over (][countryname, url]].each do,  
> rather than
> @countries.each do).
>
> Can someone help me understand why my array here (@countries) is not
> recognized?  I tried to instantiate it as a global variable  
> ($countries) and
> it still was not recognized.  If I explicitly describe an array in  
> place of
> the variable, the script works.
>
> Is it an issue with my organization format, or something else?
>
> global_page_spec.rb
> $LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..') if $0 ==  
> __FILE__
> require 'helpers/example_helper'
>
> describe "The country page for" do
>  include ExHelper
>
>  before(:all) do
>    setup
>    collect_global_countries
>  end
>
>  @countries.each do |name, link|
>    describe name do
>    it "should contain the word #{name} in the title" do
>      @browser.goto link
>      @browser.div(:id, /content-content/).text.should include(name)
>    end #it
>    end #desc
>  end #countries
>
>  after(:all) do
>    teardown
>  end #after
> end #spec
>
> example_helper.rb
> $LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..') if $0 ==  
> __FILE__
> require 'helpers/global_helper'
>
> module ExHelper
>  include GlobalHelper
>
>  def setup
>    @browser = Watir::Browser.new
>    @browser.add_checker lambda {|b| b.text.should_not include('The
> requested page could not be found.')}
>  end # setup
>
>  def collect_global_countries
>    @countries = Array.new
>    @countries.should be_empty
>
>    @browser.goto "http://www.#{$env}.com/global"
>    @browser.table(:class, /global-list/).links.each do |link|
>      @countries << [link.text, link.href]
>    end #links
>
>    @countries.should_not be_empty
>
>  def teardown
>    @browser.close
>  end # teardown
> end #module
> -- 
> View this message in context: http://old.nabble.com/RSpec-and-Watir%2C-easy-script-structure-question-tp27758607p27761159.html
> Sent from the rspec-users mailing list archive at Nabble.com.
>
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

Briefly, think of two passes over this file: the first 'parse' pass,  
and the second 'execution' pass. The #before and #it blocks run during  
the 'execution' pass, but the code in the #describe blocks runs in the  
initial 'parse' pass. Because @countries is not created until the  
before blocks run, there's no way for the 'parse' pass to know what  
value it has.

That's the basic essence of your issue. I'd recommend paring this back  
to a much simpler example, getting that working, then building up from  
there.

I'm also not sure why you're using the SUT to generate the tests, but  
that's for another thread...


cheers,
Matt

http://mattwynne.net
+447974 430184



More information about the rspec-users mailing list