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

**URL:** <https://rubytalk.org/t/how-to-use-test-unit-assertions-from-class-methods/19300>\
**Category:** ruby-talk\
**Created:** [1 July 2005 22:00 UTC](https://rubytalk.org/t/how-to-use-test-unit-assertions-from-class-methods/19300 "2005-07-01T22:00:43Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Schuerig](https://avatars.discourse-cdn.com/v4/letter/m/54ee81/32.png) [@Michael\_Schuerig](https://rubytalk.org/u/Michael_Schuerig)\
**Post date:** [1 July 2005 22:00 UTC](https://rubytalk.org/t/how-to-use-test-unit-assertions-from-class-methods/19300/1 "2005-07-01T22:00:43Z")

</div>

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](mailto:michael@schuerig.de) Are never in the line of fire  
> [http://www.schuerig.de/michael/](http://www.schuerig.de/michael/) --Ani DiFranco, Not So Soft

---

<div class="post-metadata">

**Author:** ![gabriele\_renzi5](https://avatars.discourse-cdn.com/v4/letter/g/e79b87/32.png) [@gabriele\_renzi5](https://rubytalk.org/u/gabriele_renzi5)\
**Post date:** [1 July 2005 22:35 UTC](https://rubytalk.org/t/how-to-use-test-unit-assertions-from-class-methods/19300/2 "2005-07-01T22:35:43Z")

</div>

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?

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

---

<div class="post-metadata">

**Author:** ![Ryan\_Leavengood2](https://avatars.discourse-cdn.com/v4/letter/r/8edcca/32.png) [@Ryan\_Leavengood2](https://rubytalk.org/u/Ryan_Leavengood2)\
**Post date:** [1 July 2005 22:41 UTC](https://rubytalk.org/t/how-to-use-test-unit-assertions-from-class-methods/19300/3 "2005-07-01T22:41:58Z")

</div>

Michael Schuerig said:

> But how can I use these assertions in a class method?

require 'test/unit'

class Foo  
&nbsp;&nbsp;# This:  
&nbsp;&nbsp;class \<\< self  
&nbsp;&nbsp;&nbsp;&nbsp;include Test::Unit::Assertions  
&nbsp;&nbsp;end  
&nbsp;&nbsp;# OR this:  
&nbsp;&nbsp;extend Test::Unit::Assertions

&nbsp;&nbsp;def self.do\_something  
&nbsp;&nbsp;&nbsp;&nbsp;flunk('Badness')  
&nbsp;&nbsp;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

---

<div class="post-metadata">

**Author:** ![Charles\_Steinman](https://avatars.discourse-cdn.com/v4/letter/c/7bcc69/32.png) [@Charles\_Steinman](https://rubytalk.org/u/Charles_Steinman)\
**Post date:** [1 July 2005 22:50 UTC](https://rubytalk.org/t/how-to-use-test-unit-assertions-from-class-methods/19300/4 "2005-07-01T22:50:43Z")

</div>

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  
&nbsp;&nbsp;include Test::Unit::Assertions

&nbsp;&nbsp;class \<\< self # To get the singleton class  
&nbsp;&nbsp;&nbsp;&nbsp;include Test::Unit::Assertions  
&nbsp;&nbsp;end

&nbsp;&nbsp;# Whatever methods need to use the assertions  
end

> **···**
>
> ---

---

<div class="post-metadata">

**Author:** ![Michael\_Schuerig](https://avatars.discourse-cdn.com/v4/letter/m/54ee81/32.png) [@Michael\_Schuerig](https://rubytalk.org/u/Michael_Schuerig)\
**Post date:** [1 July 2005 22:45 UTC](https://rubytalk.org/t/how-to-use-test-unit-assertions-from-class-methods/19300/5 "2005-07-01T22:45:43Z")

</div>

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?
> 
> &nbsp;&nbsp;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](mailto:michael@schuerig).de In fact, they do.  
> [Michael Schürig | Sentenced to making sense](http://www.schuerig.de/michael/) --Bertrand Russell

---

<div class="post-metadata">

**Author:** ![Michael\_Schuerig](https://avatars.discourse-cdn.com/v4/letter/m/54ee81/32.png) [@Michael\_Schuerig](https://rubytalk.org/u/Michael_Schuerig)\
**Post date:** [2 July 2005 00:00 UTC](https://rubytalk.org/t/how-to-use-test-unit-assertions-from-class-methods/19300/6 "2005-07-02T00:00:43Z")

</div>

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](mailto:michael@schuerig).de Is a blessing in disguise  
> [Michael Schürig | Sentenced to making sense](http://www.schuerig.de/michael/) --Janis Ian, From Me To You

---

<div class="post-metadata">

**Author:** ![Michael\_Schuerig](https://avatars.discourse-cdn.com/v4/letter/m/54ee81/32.png) [@Michael\_Schuerig](https://rubytalk.org/u/Michael_Schuerig)\
**Post date:** [2 July 2005 00:05 UTC](https://rubytalk.org/t/how-to-use-test-unit-assertions-from-class-methods/19300/7 "2005-07-02T00:05:43Z")

</div>

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  
> &nbsp;&nbsp;include Test::Unit::Assertions
> 
> &nbsp;&nbsp;class \<\< self # To get the singleton class  
> &nbsp;&nbsp;&nbsp;&nbsp;include Test::Unit::Assertions  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;# 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](mailto:michael@schuerig).de vacuous conclusions.  
> [Michael Schürig | Sentenced to making sense](http://www.schuerig.de/michael/) --A.O. Rorty, Explaining Emotions

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf1](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@Joel\_VanderWerf1](https://rubytalk.org/u/Joel_VanderWerf1)\
**Post date:** [2 July 2005 19:07 UTC](https://rubytalk.org/t/how-to-use-test-unit-assertions-from-class-methods/19300/8 "2005-07-02T19:07:38Z")

</div>

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  
> > &nbsp;&nbsp;&nbsp;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 🙂

---

<div class="post-metadata">

**Author:** ![Daniel\_Brockman](https://avatars.discourse-cdn.com/v4/letter/d/ed8c4c/32.png) [@Daniel\_Brockman](https://rubytalk.org/u/Daniel_Brockman)\
**Post date:** [2 July 2005 22:56 UTC](https://rubytalk.org/t/how-to-use-test-unit-assertions-from-class-methods/19300/9 "2005-07-02T22:56:35Z")

</div>

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.

&nbsp;&nbsp;&nbsp;void  
&nbsp;&nbsp;&nbsp;rb\_extend\_object(obj, module)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VALUE obj, module;  
&nbsp;&nbsp;&nbsp;{  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rb\_include\_module(rb\_singleton\_class(obj), module);  
&nbsp;&nbsp;&nbsp;}

> **···**
>
> --  
> Daniel Brockman \<daniel@brockman.se\>
