I need to write tests for some behavior that needs to check a
constant:
if (MOD_RUBY)
… do stuff
else
… do something else
end
I can do it by having this sort of thing:
MOD_RUBY = true
and
MOD_RUBY = false
but I don’t want to get the ruby warnings. What’s another way for me
to mock this constant?
Thanks,
~ Patrick
Patrick May wrote:
I need to write tests for some behavior that needs to check a
constant:
if (MOD_RUBY)
… do stuff
else
… do something else
end
I can do it by having this sort of thing:
MOD_RUBY = true
and
MOD_RUBY = false
but I don’t want to get the ruby warnings. What’s another way for me
to mock this constant?
Not sure I understand the problem, but if you’re trying to redefine a
constant in a way that won’t generate warnings, how about using modules?
module M1; K = 1; end
module M2; K = 2; end
etc., for as many times as you need to change K
include M1 # now K == 1
include M2 # now K == 2
But repeating ‘include M1’ doesn’t go back to K == 1. Ruby checks to see
if the module has already been included.
If you need to be able to change the constant more dynamically, you can
do something like:
m = Module.new
m.const_set(:K, variable)
include m
Though I’m not sure what your goal is, how about:
(1) ignore that warning.
(2) do testing in different namespaces if it is possible.
(3) use Module#remove_const:
class C
MOD_RUBY = true
remove_const :MOD_RUBY
MOD_RUBY = false
end
(4) use a class variable instead of a constant in the product.
Hope this helps,
– Gotoken
···
At Thu, 8 Aug 2002 13:25:41 +0900, Patrick May wrote:
I need to write tests for some behavior that needs to check a
constant:
if (MOD_RUBY)
… do stuff
else
… do something else
end
I can do it by having this sort of thing:
MOD_RUBY = true
and
MOD_RUBY = false
but I don’t want to get the ruby warnings. What’s another way for me
to mock this constant?
Hi,
Patrick May patrick-may@monmouth.com writes:
I need to write tests for some behavior that needs to check a
constant:
if (MOD_RUBY)
… do stuff
else
… do something else
end
If you want to judge whether it is called from mod_ruby,
you can use `defined?’ .
if defined? MOD_RUBY
… do stuff
else
… do something else
end
···
–
eban
GOTO Kentaro gotoken@notwork.org wrote in message news:200208080613.g786DGFI000424@miso.k.notwork.org…
Though I’m not sure what your goal is, how about:
(1) ignore that warning.
(2) do testing in different namespaces if it is possible.
(3) use Module#remove_const:
class C
MOD_RUBY = true
remove_const :MOD_RUBY
MOD_RUBY = false
end
This looks promising.
(4) use a class variable instead of a constant in the product.
Hope this helps,
– Gotoken
I am trying to write unit tested parsing code for a cgi module.
Different parsing routines need to be run in different situations; the
only reliable way to check whether MOD_RUBY is in use is to do if
defined? MOD_RUBY or if MOD_RUBY.
I have to be able to turn the constant on and off, b/c I have to test
other parsing situations as well.
~ Patrick