Testing for mod_ruby (was "how do i mock the presence / absence of a constant w/out getting warnings?")

Currently, the only reliable way to test for the mod_ruby enviroment
is to look for the constant, as in:

if (MOD_RUBY) then … end

or

if defined? MOD_RUBY then … end

I am writing unit tested code which needs to behave differently
depending on whether MOD_RUBY is present or absent. Currently, such
behavior is near impossible to mock, b/c to ruby constants are exactly
that – constant. I can’t “undeclare” a top level constant to
teardown the MOD_RUBY tests.

Perhaps if mod_ruby set a value in the ENV table as a flag this would
be easier? Are there any reasons mod_ruby shouldn’t set such a flag?

Thanks,

Patrick

Hi,

···

At Fri, 9 Aug 2002 05:03:06 +0900, Patrick May wrote:

I am writing unit tested code which needs to behave differently
depending on whether MOD_RUBY is present or absent. Currently, such
behavior is near impossible to mock, b/c to ruby constants are exactly
that – constant. I can’t “undeclare” a top level constant to
teardown the MOD_RUBY tests.

Use Module#remove_const.

Object.module_eval {remove_const(:MOD_RUBY)}


Nobu Nakada

Thanks for your suggestion, sorry it took me a while to respond.

nobu.nokada@softhome.net wrote in message news:200208082359.g78Nxnr19485@sharui.nakada.kanuma.tochigi.jp

Use Module#remove_const.

Object.module_eval {remove_const(:MOD_RUBY)}

Thanks, this worked for me. Unfortunately, after I did

Object.const_set( :MOD_RUBY, true )

in the context of the test, the check in my code (different context):

if (defined? MOD_RUBY)

continued to be false. If I checked in the body of the test method,
this check returned true. Is this the expected behaviour?

I haven’t investigated this to deeply, b/c I’ve been working on other
problems and I’ve setup a work around for this issue.

Thanks for your help,

Patrick

Hi,

···

At Sun, 11 Aug 2002 13:04:29 +0900, Patrick May wrote:

Thanks, this worked for me. Unfortunately, after I did

Object.const_set( :MOD_RUBY, true )

in the context of the test, the check in my code (different context):

if (defined? MOD_RUBY)

continued to be false. If I checked in the body of the test method,
this check returned true. Is this the expected behaviour?

What are the contexts?


Nobu Nakada

nobu.nokada@softhome.net wrote in message news:200208110541.g7B5fvr27108@sharui.nakada.kanuma.tochigi.jp

Hi,

Thanks, this worked for me. Unfortunately, after I did

Object.const_set( :MOD_RUBY, true )

in the context of the test, the check in my code (different context):

if (defined? MOD_RUBY)

continued to be false. If I checked in the body of the test method,
this check returned true. Is this the expected behaviour?

What are the contexts?

This is roughly what was happening. A bit later this week, I’m going
to sit down and really examine this problem. I was doing something
like this:

(note that this code is from memory. I’m not sure if this will run
correctly or if it will reproduce my problem)

class TestRequest < RUNIT::TestCase
def teardown
if (defined? MOD_RUBY)
Object.module_eval{ remove_const :MOD_RUBY }
end
end

def test_parse_with_mod_ruby
    Object.const_set( :MOD_RUBY, true )
    assert( defined? MOD_RUBY )       # <= this was true
    assert( CGI::Request.mod_ruby? )  # <= this was false?
end

end

class CGI
class Request
def Request.mod_ruby?
defined? MOD_RUBY
end
end
end

By the end of this week, I’m going to sit down and examine this
problem further. Then I will double check my code, and try to get a
clear demonstration of my problem.

Thanks,

Patrick

···

At Sun, 11 Aug 2002 13:04:29 +0900, > Patrick May wrote:

Hi,

    assert( defined? MOD_RUBY )       # <= this was true
    assert( CGI::Request.mod_ruby? )  # <= this was false?

RUNIT::Assert#assert requires true or false exactly, but
defined? operator returns string representing object type or
nil.

This works.

def test_parse_with_mod_ruby
    Object.const_set(:MOD_RUBY, true)
    assert(!!defined?(MOD_RUBY))
    assert(!!CGI::Request.mod_ruby?)
end

def test_parse_without_mod_ruby
    assert(!defined?(MOD_RUBY))
    assert(!CGI::Request.mod_ruby?)
end
···

At Mon, 12 Aug 2002 12:06:39 +0900, Patrick May wrote:


Nobu Nakada