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

  • Thread starter Michael Schuerig
  • Start date
M

Michael Schuerig

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
 
G

gabriele renzi

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
 
M

Michael Schuerig

gabriele said:
Michael Schuerig ha scritto:

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
 
C

Charles Steinman

Michael said:
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
---
 
M

Michael Schuerig

Charles said:
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
 
J

Joel VanderWerf

Michael said:
Charles Steinman wrote:




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 :)
 
D

Daniel Brockman

Joel VanderWerf said:
Using #include on the class's signleton class is the same as using
#extend on the class itself, IIRC

Exactly.

void
rb_extend_object(obj, module)
VALUE obj, module;
{
rb_include_module(rb_singleton_class(obj), module);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top