W
weathercoach
Hello.
This is my first attempt in trying to use the ruby library test/
unit. Additionally my programming and ruby skills are still around
the beginner stage.
I have a unix utility which I've written in ruby and now that it
actually does some work I want to be able to test it. My first
attempt at a unit test was to check the value of a class variable but
this results in the following error
NameError: uninitialized class variable @@bad_users in FindUsersTest
Here is a class and test which demonstrates what I'm trying to
do.
-- start class
class Simple
@@whatisit = "false"
def parse(n)
@@whatisit = true if n =~ /yes/
end
end
--- start test
require 'simple'
require 'test/unit'
class SimpleTest < Test::Unit::TestCase
def test_parse
v = Simple.new
v.parse("yes")
assert @@whatisit
end
end
--- run test
../test_simple.rb
Started
E
Finished in 0.003813 seconds.
1) Error:
test_parse(SimpleTest):
NameError: uninitialized class variable @@whatisit in SimpleTest
./test_simple.rb:14:in `test_parse'
1 tests, 0 assertions, 0 failures, 1 errors
I then added this method into the simple class above
def return_whatisit
@@whatisit
end
Then in my test created a Simple object (v = Simple.new) and then
called the return_whatisit method (eg: v.return_whatisit). I dont
really want to have to add extra methods to the code to support the
retreival of class variables from within the tests. I dont
understand why I'm not able to access the class variable @@whatisit
from within the test script? Is it possible to extend the Simple
class from within the test file itself?
For example iin the test file I would like to add this method to
the Simple class
def return_whatisit
@@whatisit
end
So if you have any tips on how to best approach this situation I
would appreciate it. I figure if I get a better understanding of
using and writing tests that will influence how I write scripts down
the road.
TIA. G
This is my first attempt in trying to use the ruby library test/
unit. Additionally my programming and ruby skills are still around
the beginner stage.
I have a unix utility which I've written in ruby and now that it
actually does some work I want to be able to test it. My first
attempt at a unit test was to check the value of a class variable but
this results in the following error
NameError: uninitialized class variable @@bad_users in FindUsersTest
Here is a class and test which demonstrates what I'm trying to
do.
-- start class
class Simple
@@whatisit = "false"
def parse(n)
@@whatisit = true if n =~ /yes/
end
end
--- start test
require 'simple'
require 'test/unit'
class SimpleTest < Test::Unit::TestCase
def test_parse
v = Simple.new
v.parse("yes")
assert @@whatisit
end
end
--- run test
../test_simple.rb
Started
E
Finished in 0.003813 seconds.
1) Error:
test_parse(SimpleTest):
NameError: uninitialized class variable @@whatisit in SimpleTest
./test_simple.rb:14:in `test_parse'
1 tests, 0 assertions, 0 failures, 1 errors
I then added this method into the simple class above
def return_whatisit
@@whatisit
end
Then in my test created a Simple object (v = Simple.new) and then
called the return_whatisit method (eg: v.return_whatisit). I dont
really want to have to add extra methods to the code to support the
retreival of class variables from within the tests. I dont
understand why I'm not able to access the class variable @@whatisit
from within the test script? Is it possible to extend the Simple
class from within the test file itself?
For example iin the test file I would like to add this method to
the Simple class
def return_whatisit
@@whatisit
end
So if you have any tips on how to best approach this situation I
would appreciate it. I figure if I get a better understanding of
using and writing tests that will influence how I write scripts down
the road.
TIA. G