instance variable question in test/unit TestCase

  • Thread starter Nicholas Van Weerdenburg
  • Start date
N

Nicholas Van Weerdenburg

Hi all,

test/unit is super cool.I was blown away by how test/unit automatically
looks in ObjectSpace and creates a test suite for all tests found, and
then runs them. I was wondering how it was possible, so I looked at
unit.rb-

at_exit{exit(Test::Unit::AutoRunner.run($0)) unless($! || Test::Unit.run?)}

Cool!

A couple of questions:

1. A more general question: how do I initialize instance variables
without an "initialize" method? In my test case, I want instance
variables, but placing "@myvar=1" outside a method doesn't seem to work
( I dimly recall that this is a class instance variable or something
like that). The reason I want to do this is that in subclassing
Test::Unit::TestCase I don't know offhand what it's initialize method
looks like (a no arg "initialize" gives an argment number error) and
don't want to learn just for this reason.

I just thought of trying the following-

def initialize *args
super *args
@myvar=1
end

and it worked (I think). Cool.

My other option is the setup method, since that is called for before all
test methods. Are there any other options?

2. where do I find the will to return to programming in Java now that
I'm having so much fun in Ruby?

Thanks,
Nick
 
J

Joel VanderWerf

Nicholas said:
1. A more general question: how do I initialize instance variables
without an "initialize" method? In my test case, I want instance
variables, but placing "@myvar=1" outside a method doesn't seem to work
( I dimly recall that this is a class instance variable or something
like that). The reason I want to do this is that in subclassing
Test::Unit::TestCase I don't know offhand what it's initialize method
looks like (a no arg "initialize" gives an argment number error) and
don't want to learn just for this reason.

I just thought of trying the following-

def initialize *args
super *args
@myvar=1
end

and it worked (I think). Cool.

My other option is the setup method, since that is called for before all
test methods. Are there any other options?

Use the ||= idiom. I like it because it can keep the initialization near
the point of use.

def foo
@myvar ||= 1 # initialize @myvar, but only if it's nil
end

Of course, this assumes that nil is not an expected value for the
variable. Otherwise, you can use

@myvar = 1 unless defined?(@myvar)
2. where do I find the will to return to programming in Java now that
I'm having so much fun in Ruby?

Dunno.. write a java code generator in ruby?
 
N

Nicholas Van Weerdenburg

@myvar ||= 1 # initialize @myvar, but only if it's nil
@myvar = 1 unless defined?(@myvar)

Ah, the first idiom is neat. I hadn't seen that one. Unfortunately, the
issue I still have is that I don't have a single place where I want to
set these- the variable is in many methods.

Hmmm...Java code generator in Ruby. I like it!

Thanks,
Nick
 
J

Joel VanderWerf

Nicholas said:
@myvar ||= 1 # initialize @myvar, but only if it's nil
@myvar = 1 unless defined?(@myvar)

Ah, the first idiom is neat. I hadn't seen that one. Unfortunately, the
issue I still have is that I don't have a single place where I want to
set these- the variable is in many methods.

Good point. In that case, define a method:

def myvar
@myvar ||= 1
end

It works more or less as if you defined it with attr_reader.

Then make sure you always call the method rather than referencing the
instance var directly.
 
E

Eric Hodel

--CqfQkoYPE/jGoa5Q
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable
Hi all,
=20
test/unit is super cool.I was blown away by how test/unit automatically= =20
looks in ObjectSpace and creates a test suite for all tests found, and=20
then runs them. I was wondering how it was possible, so I looked at=20
unit.rb-
=20
at_exit{exit(Test::Unit::AutoRunner.run($0)) unless($! || Test::Unit.run?= )}
=20
Cool!
=20
A couple of questions:
=20
1. A more general question: how do I initialize instance variables=20
without an "initialize" method? In my test case, I want instance=20
variables, but placing "@myvar=3D1" outside a method doesn't seem to work= =20
( I dimly recall that this is a class instance variable or something=20
like that). The reason I want to do this is that in subclassing=20
Test::Unit::TestCase I don't know offhand what it's initialize method=20
looks like (a no arg "initialize" gives an argment number error) and=20
don't want to learn just for this reason.

Use the setup and teardown methods. Here's how a test case gets run:

test_case =3D YourTestCase.new
test_case.setup
test_case.test_something
test_case.teardown

(A fresh, new YourTestCase instance is created every time.)
My other option is the setup method, since that is called for before all= =20
test methods. Are there any other options?

Yes :)
2. where do I find the will to return to programming in Java now that=20
I'm having so much fun in Ruby?

You can find a unit test suite for Java, and write code in Groovy. Word
on the street says Groovy is much like Ruby.

--=20
Eric Hodel - (e-mail address removed) - http://segment7.net
All messages signed with fingerprint:
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04


--CqfQkoYPE/jGoa5Q
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (FreeBSD)

iD8DBQFA9T58MypVHHlsnwQRAk+kAJ4/iwMjdkWZsOOc3Ir6qPqXEtuiZACeLuio
DFghXQWIyP4OGLCBb2VcNAI=
=60bu
-----END PGP SIGNATURE-----

--CqfQkoYPE/jGoa5Q--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top