You're defining the @countries instance variable outside of any method. You're making @countries an instance variable of whatever 'self' is in the describe block (which appears to be a class or a module), which is not the same thing that your methods are being called on (presumably an instance of that class or a class including that module). If you want 'countries' to be the same across all instances of the class, use a class variable. If you want it to be the same throughout the program, use a global. If it should be per-instance, use an instance variable defined in some method of the class (#initialize, most likely).
On Tue, Mar 2, 2010 at 16:11, Adam Reed <[email protected]> wrote: > 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). In a nutshell, the script goes to a static page, > collects the links, visits each linked page and verifies an item - it > has been significantly dumbed down for the purpose of this question > and I have tested this simplified version to make sure it gives the > same output. > > 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. Is it an issue with my > organization/format, or something else? > > **** Again, if I explicitly describe an array in place of the > variable, the script works ****. > > 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 > > The Error: > > global_page_spec.rb:12: undefined method `each' for nil:NilClass > (NoMethodError) > from C:/Ruby/lib/ruby/gems/1.8/gems/rspec-1.3.0/lib/spec/example/ > example_group_methods.rb:183:in `module_eval' > from C:/Ruby/lib/ruby/gems/1.8/gems/rspec-1.3.0/lib/spec/example/ > example_group_methods.rb:183:in `subclass' > from C:/Ruby/lib/ruby/gems/1.8/gems/rspec-1.3.0/lib/spec/example/ > example_group_methods.rb:55:in `describe' > from C:/Ruby/lib/ruby/gems/1.8/gems/rspec-1.3.0/lib/spec/example/ > example_group_factory.rb:31:in `create_example_group' > from > C:/Ruby/lib/ruby/gems/1.8/gems/rspec-1.3.0/lib/spec/dsl/main.rb: > 28:in `describe' > from global_page_spec.rb:4 > > -- > You received this message because you are subscribed to the Google Groups > "Watir General" group. > To post to this group, send email to [email protected] > Before posting, please read the following guidelines: > http://wiki.openqa.org/display/WTR/Support > To unsubscribe from this group, send email to > [email protected]<watir-general%[email protected]> > For more options, visit this group at > http://groups.google.com/group/watir-general > -- You received this message because you are subscribed to the Google Groups "Watir General" group. To post to this group, send email to [email protected] Before posting, please read the following guidelines: http://wiki.openqa.org/display/WTR/Support To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/watir-general
