Wrong number of arguments in initialize

I tried to initalize my methods and I get an error
def initialize(name,browser,username)
@s_name=name
@browser=browser
@username=username
end #def initialize(dc)
attr_accessor :name,:browser,:username

C:/RubyInstall/ruby/lib/ruby/1.8/test/unit/testcase.rb:59:in
`initialize’: wrong number of arguments (1 for 3) (ArgumentError)

I honestly dont get it now, this is silly

Arti S. wrote:

I tried to initalize my methods and I get an error
def initialize(name,browser,username)

You define intialize to ask for 3 arguments, you don’t give any of them
defaults…

C:/RubyInstall/ruby/lib/ruby/1.8/test/unit/testcase.rb:59:in
`initialize’: wrong number of arguments (1 for 3) (ArgumentError)

You don’t show where you call the initialize method, but the error says
you only gave one argument, and that it was called on line 59…

I honestly dont get it now, this is silly

I get bit by this one all the time :slight_smile:

but what’s the test code you’re executing that raises the error?

Arti S. wrote:

I tried to initalize my methods and I get an error
def initialize(name,browser,username)
@s_name=name
@browser=browser
@username=username
end #def initialize(dc)
attr_accessor :name,:browser,:username

C:/RubyInstall/ruby/lib/ruby/1.8/test/unit/testcase.rb:59:in
`initialize’: wrong number of arguments (1 for 3) (ArgumentError)

Open that file in a text editor, go to line 59, and you’ll see

      catch(:invalid_test) do
        suite << new(test)         #### ERROR RAISED HERE
      end

Then scroll up and you’ll see you’re inside the ‘suite’ class method of
class Test::Unit::TestCase, and higher up you’ll see

  def initialize(test_method_name)
    ...

That is, when a Test::Unit::TestCase instance is created, it takes a
single argument which is the test method name.

Almost certainly you should not be meddling with the ‘initialize’ method
of Test::Unit::TestCase. What exactly are you trying to do? I guess you
want to be able to run the same test suite multiple times with different
parameters? The simplest way may be

def setup
@s_name=ENV[‘TEST_S_NAME’]
@browser=ENV[‘TEST_BROWSER’]
@username=ENV[‘TEST_USERNAME’]
end

Then you can do

rake test TEST_S_NAME=xxx TEST_BROWSER=yyy TEST_USERNAME=zzz

An alternative way is to have a separate class for each of your
scenarios, and share the test methods using subclassing or module
mixins. Then running the suite will run all of the variations in one go.

require ‘test/unit’

class TestFoo < Test::Unit::TestCase
def setup
@browser = “foo browser”
end
def test_it
puts “Browser is #{@browser}”
assert true
end
end

class TestBar < TestFoo
def setup
@browser = “bar browser”
end
end