[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Writing test cases/suites

Johan Nilsson

1/19/2005 9:08:00 AM

Hi,

I pretty often use small helper classes for my unit tests; call them simple
mock objects or what you like. A trivial example, for the sake of
illustration:

--- tc_myclass.rb ---

require 'test/unit'

class CountingClient
attr_reader :times_called
def initialize
@times_called = 0
end
def on_cb(whatever)
@times_called += 1
end
end

class TC_MyClass < Test::Unit::TestCase

def test_no_of_dispatched_calls

mc = MyClass.new
cc = CountingClient.new

mc.add_listener(cc)
mc.dispatch("A")
mc.dispatch("B")

assert_equal(2, cc.times_called)

end

end

-----------------
My problem comes now when I create a test suite, e.g.

--- ts_myclasses.rb ---
require 'test/unit'
require 'tc_myclass'
require 'tc_myotherclass'
-----------------------

Imagine now that they both define test helper classes named CountingClient,
and that the two definitions of CountingClient differ (well, otherwise
they'd be in a separate file). How should I handle this without naming the
classes "TC_MyClass_CountingClient" etc...

What I did in C++ (for those who are familiar with that) was to always
define such classes inside an anonymous namespace, which means that they are
only accessible within the current compilation unit. A rubyized comparison
would be:

module # Note: no name

class CountingClient
...
end # CountingClient

end # anonymous module

The class CountingClient should then only be accessible within the current
..rb file and take precedence over previous definitions. But no such beast is
available - right?


Regards // Johan

3 Answers

Nathaniel Talbott

1/19/2005 1:36:00 PM

0

On Jan 19, 2005, at 04:06, Johan Nilsson wrote:

> The class CountingClient should then only be accessible within the
> current ..rb file and take precedence over previous definitions. But
> no such beast is available - right?

I usually just nest such helpers within the test class, like so:

class T < Test::Unit::TestCase
class Helper
...
end

...
end

That way the helper is only (directly) visible to the test it's being
used by. Will that work for you?


Nathaniel
Terralien, Inc.

<:((><



Johan Nilsson

1/20/2005 7:27:00 AM

0


"Nathaniel Talbott" <nathaniel@talbott.ws> wrote in message
news:160E114C-6A1F-11D9-8A83-000A95CD7A8E@talbott.ws...
> On Jan 19, 2005, at 04:06, Johan Nilsson wrote:
>
>> The class CountingClient should then only be accessible within the
>> current ..rb file and take precedence over previous definitions. But no
>> such beast is available - right?
>
> I usually just nest such helpers within the test class, like so:
>
> class T < Test::Unit::TestCase
> class Helper
> ...
> end
>
> ...
> end
>
> That way the helper is only (directly) visible to the test it's being used
> by. Will that work for you?

Yes, of course, I wasn't even aware that Ruby supported nested classes
(still learning). As Class is a kind of Module I should have thought about
it though.

I'd still prefer the 'anonymous' module, but this will work.

Thanks!

// Johan

nobu.nokada

1/22/2005 12:31:00 AM

0

Hi,

At Thu, 20 Jan 2005 16:26:00 +0900,
Johan Nilsson wrote in [ruby-talk:127304]:
> I'd still prefer the 'anonymous' module, but this will work.

helper = Module.new do
class CountingClient
attr_reader :times_called
def initialize
@times_called = 0
end
def on_cb(whatever)
@times_called += 1
end
end
end

TC_MyClass.class_eval {include(helper)}

--
Nobu Nakada