From: Robert Klemme Date: 2013-04-09T02:01:15+09:00 Subject: Re: Accessing global variable in Unit Tests --90e6ba3090b05d6c8e04d9dc62b4 Content-Type: text/plain; charset=ISO-8859-1 On Mon, Apr 8, 2013 at 6:33 PM, T- Di wrote: > Hi, > > I'm having problems using a global variable defined in the my test > class, which then is referenced in the libraries file. I'm using Ruby > 1.9.3p392 and test-unit 2.5.4 > > Here is the code that runs the tests: > > require 'rubygems' > gem 'test-unit' > require 'test/unit' > require 'ci/reporter/rake/test_unit_loader' > load '../lib/functions.rb' > require 'watir' > > class Test_002 < Test::Unit::TestCase > > include Functions > > class << self > def startup > I think this should be called "setup" and be an instance method and not a class method. > @browser = Watir::Browser.new :ie > @browser.speed = :fast > end > > def shutdown > This should be called "teardown". > @browser.close > end > end > > def test_001_login > login('some_url', 'some_user', 'some_passw') > end > end > > > And this is part of the library that contains the login function: > > require 'rubygems' > > module Functions > > def login(url, user, passw) > @browser.goto(url) > ... > end > end > > > This is the output: > Started > E > > =============================================================================== > Error: test_001_login(Test_002) > NoMethodError: undefined method `goto' for nil:NilClass > (...) > 23: end > 24: > 25: def test_001_login > => 26: login('some_url', 'some_user', 'some_passw') > 27: end > 28: > 29: end > > =============================================================================== > > > Finished in 3.0043 seconds. > > 1 tests, 0 assertions, 0 failures, 1 errors, 0 pendings, 0 omissions, 0 > notifications > 0% passed > > 0.33 tests/s, 0.00 assertions/s > > Any ideas? > @browser is not initialized. See http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/ --90e6ba3090b05d6c8e04d9dc62b4 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable



On Mon, Apr 8, 2013 at 6:33 PM, T- Di <lists@ruby-forum.com= > wrote:
Hi,

I'm having problems using a global variable defined in the my test
class, which then is referenced in the libraries file. I'm using Ruby 1.9.3p392 and test-unit 2.5.4

Here is the code that runs the tests:

require 'rubygems'
gem 'test-unit'
require 'test/unit'
require 'ci/reporter/rake/test_unit_loader'
load '../lib/functions.rb'
require 'watir'

class Test_002 < Test::Unit::TestCase

=A0 include Functions

=A0 class << self
=A0 =A0def startup

I think this s= hould be called "setup" and be an instance method and not a class= method.
=A0
=A0 =A0 =A0 @browser =3D Watir::Browser.new :ie
=A0 =A0 =A0 @browser.speed =3D :fast
=A0 =A0 end

=A0 =A0 def shutdown

This should = be called "teardown".
=A0
=A0 =A0 =A0 @browser.close
=A0 =A0 end
=A0 end

=A0 def test_001_login
=A0 =A0 login('some_url', 'some_user', 'some_passw'= )
=A0 end
end


And this is part of the library that contains the login function:

require 'rubygems'

=A0module Functions

=A0 def login(url, user, passw)
=A0 =A0 @browser.goto(url)
=A0 =A0 ...
=A0 end
end


This is the output:
Started
E
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D
Error: test_001_login(Test_002)
=A0 NoMethodError: undefined method `goto' for nil:NilClass
(...)
=A0 =A0 =A023: =A0 end
=A0 =A0 =A024:
=A0 =A0 =A025: =A0 def test_001_login
=A0 =3D> 26: =A0 =A0 login('some_url', 'some_user', '= ;some_passw')
=A0 =A0 =A027: =A0 end
=A0 =A0 =A028:
=A0 =A0 =A029: end
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D


Finished in 3.0043 seconds.

1 tests, 0 assertions, 0 failures, 1 errors, 0 pendings, 0 omissions, 0
notifications
0% passed

0.33 tests/s, 0.00 assertions/s

Any ideas?
=A0
@browser is not initial= ized. =A0See


Kind regards

robert



--
rememb= er.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
--90e6ba3090b05d6c8e04d9dc62b4--