[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

How to use Test::Unit::Assertions from class methods?

Michael Schuerig

7/1/2005 10:00:00 PM


For unit tests I've written a mock object that should check its
arguments against expected arguments using assertions from
Test::Unit::Assertions. For instance methods I can include
Test::Unit::Assertions in the mock class, that's easy. But how can I
use these assertions in a class method?

Michael

--
Michael Schuerig Those who call the shots
mailto:michael@schuerig.de Are never in the line of fire
http://www.schuerig.d... --Ani DiFranco, Not So Soft

8 Answers

gabriele renzi

7/1/2005 10:34:00 PM

0

Michael Schuerig ha scritto:
> For unit tests I've written a mock object that should check its
> arguments against expected arguments using assertions from
> Test::Unit::Assertions. For instance methods I can include
> Test::Unit::Assertions in the mock class, that's easy. But how can I
> use these assertions in a class method?

IIUC, the #extend method is what you need, instead of #include

Michael Schuerig

7/1/2005 10:42:00 PM

0

gabriele renzi wrote:

> Michael Schuerig ha scritto:
>> For unit tests I've written a mock object that should check its
>> arguments against expected arguments using assertions from
>> Test::Unit::Assertions. For instance methods I can include
>> Test::Unit::Assertions in the mock class, that's easy. But how can I
>> use these assertions in a class method?
>
> IIUC, the #extend method is what you need, instead of #include

Yes, thanks, that is it. I've found a short note to that effect on p.385
of PickAxe2, also.

Michael

--
Michael Schuerig Most people would rather die than think.
mailto:michael@schuerig.de In fact, they do.
http://www.schuerig.d... --Bertrand Russell

Ryan Leavengood

7/1/2005 10:42:00 PM

0

Michael Schuerig said:
>
> But how can I use these assertions in a class method?

require 'test/unit'

class Foo
# This:
class << self
include Test::Unit::Assertions
end
# OR this:
extend Test::Unit::Assertions

def self.do_something
flunk('Badness')
end
end

Foo::do_something
__END__

You will need to rescue on the Test::Unit::AssertionFailedError if you
don't want your first failed test taking the whole program down.

Ryan


acharlieblue

7/1/2005 10:47:00 PM

0


Michael Schuerig wrote:
> For unit tests I've written a mock object that should check its
> arguments against expected arguments using assertions from
> Test::Unit::Assertions. For instance methods I can include
> Test::Unit::Assertions in the mock class, that's easy. But how can I
> use these assertions in a class method?

Include them in the mock class's singleton class.

--- (untested code, but should work) ---
class MockClass
include Test::Unit::Assertions

class << self # To get the singleton class
include Test::Unit::Assertions
end

# Whatever methods need to use the assertions
end
---

Michael Schuerig

7/2/2005 12:01:00 AM

0

Ryan Leavengood wrote:

> You will need to rescue on the Test::Unit::AssertionFailedError if you
> don't want your first failed test taking the whole program down.

No, that's not a problem. I'm using the class only in unit tests where
Test::Unit handles these things. It's just a mock object that poses for
some other class and only checks that calls are as expected.

Michael

--
Michael Schuerig They tell you that the darkness
mailto:michael@schuerig.de Is a blessing in disguise
http://www.schuerig.d... --Janis Ian, From Me To You

Michael Schuerig

7/2/2005 12:02:00 AM

0

Charles Steinman wrote:

>
> Michael Schuerig wrote:
>> For unit tests I've written a mock object that should check its
>> arguments against expected arguments using assertions from
>> Test::Unit::Assertions. For instance methods I can include
>> Test::Unit::Assertions in the mock class, that's easy. But how can I
>> use these assertions in a class method?
>
> Include them in the mock class's singleton class.
>
> --- (untested code, but should work) ---
> class MockClass
> include Test::Unit::Assertions
>
> class << self # To get the singleton class
> include Test::Unit::Assertions
> end
>
> # Whatever methods need to use the assertions
> end
> ---

Thanks, yes, that works, too. I'll need to work with the Ruby object
model some more to become really comfortable with it.

Michael

--
Michael Schuerig Airtight arguments have
mailto:michael@schuerig.de vacuous conclusions.
http://www.schuerig.d... --A.O. Rorty, Explaining Emotions

Joel VanderWerf

7/2/2005 7:08:00 PM

0

Michael Schuerig wrote:
> Charles Steinman wrote:
>
>
>>Michael Schuerig wrote:
>>
>>>For unit tests I've written a mock object that should check its
>>>arguments against expected arguments using assertions from
>>>Test::Unit::Assertions. For instance methods I can include
>>>Test::Unit::Assertions in the mock class, that's easy. But how can I
>>>use these assertions in a class method?
>>
>>Include them in the mock class's singleton class.
>>
>>--- (untested code, but should work) ---
>>class MockClass
>> include Test::Unit::Assertions
>>
>> class << self # To get the singleton class
>> include Test::Unit::Assertions
>> end
>>
>> # Whatever methods need to use the assertions
>>end
>>---
>
>
> Thanks, yes, that works, too. I'll need to work with the Ruby object
> model some more to become really comfortable with it.

Using #include on the class's signleton class is the same as using
#extend on the class itself, IIRC, so it's not really as confusing as it
sounds at first :)



Daniel Brockman

7/2/2005 10:57:00 PM

0