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
Nicholas Van Weerdenburg wrote:
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?
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.
Use the setup and teardown methods. Here's how a test case gets run:
test_case = 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
test methods. Are there any other options?
Yes
2. where do I find the will to return to programming in Java now that
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.
···
Nicholas Van Weerdenburg (nick@activehitconsulting.com) wrote:
--
Eric Hodel - drbrain@segment7.net - http://segment7.net
All messages signed with fingerprint:
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04
@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
Joel VanderWerf wrote:
···
Nicholas Van Weerdenburg wrote:
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?
Nicholas Van Weerdenburg wrote:
@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.
So far, that's probably the best solution.
Thanks,
Nick
Joel VanderWerf wrote:
···
Nicholas Van Weerdenburg wrote:
@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.