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.de/michael/ --Ani DiFranco, Not So Soft
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 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
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
···
---
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.
Michael Schürig | Sentenced to making sense --Bertrand Russell
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
Michael Schürig | Sentenced to making sense --Janis Ian, From Me To You
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.
Michael Schürig | Sentenced to making sense --A.O. Rorty, Explaining Emotions
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 
Joel VanderWerf <vjoel@path.berkeley.edu> writes:
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);
}
···
--
Daniel Brockman <daniel@brockman.se>