# Using RSpec to Describe Included Module Behavior

**URL:** <https://rubytalk.org/t/using-rspec-to-describe-included-module-behavior/43850>\
**Category:** ruby-talk\
**Created:** [17 January 2008 18:23 UTC](https://rubytalk.org/t/using-rspec-to-describe-included-module-behavior/43850 "2008-01-17T18:23:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Starr\_Trader](https://avatars.discourse-cdn.com/v4/letter/s/46a35a/32.png) [@Starr\_Trader](https://rubytalk.org/u/Starr_Trader)\
**Post date:** [17 January 2008 18:23 UTC](https://rubytalk.org/t/using-rspec-to-describe-included-module-behavior/43850/1 "2008-01-17T18:23:31Z")

</div>

Greeting all,

I'm having difficulties with RSpec. I have a module that adds some  
interface functions to a class (a la Rails) and I want to test their  
functionality. (BDD and all you know)

I'm not sure how to describe a module like this. It is not a class  
where I can create an instance directly for testing, I somehow need to  
create a Class that can have this module included in it.

Any ideas?

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Judson\_Lester1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/judson_lester1/32/1968_2.png) [@Judson\_Lester1](https://rubytalk.org/u/Judson_Lester1)\
**Post date:** [17 January 2008 20:19 UTC](https://rubytalk.org/t/using-rspec-to-describe-included-module-behavior/43850/2 "2008-01-17T20:19:27Z")

</div>

Try something like

before do  
&nbsp;&nbsp;@test\_me = Object.new  
&nbsp;&nbsp;@test\_me.extend MyCoolModule  
end

> **···**
>
> On Jan 17, 2008 10:23 AM, Starr Trader \<startrader@inbox.com\> wrote:
> 
> > Greeting all,
> > 
> > I'm having difficulties with RSpec. I have a module that adds some  
> > interface functions to a class (a la Rails) and I want to test their  
> > functionality. (BDD and all you know)
> > 
> > I'm not sure how to describe a module like this. It is not a class  
> > where I can create an instance directly for testing, I somehow need to  
> > create a Class that can have this module included in it.
> > 
> > Any ideas?
> 
> --  
> Your subnet is currently 169.254.0.0/16. You are likely to be eaten by a grue.

---

<div class="post-metadata">

**Author:** ![Pat\_Maddox1](https://avatars.discourse-cdn.com/v4/letter/p/ccd318/32.png) [@Pat\_Maddox1](https://rubytalk.org/u/Pat_Maddox1)\
**Post date:** [18 January 2008 04:16 UTC](https://rubytalk.org/t/using-rspec-to-describe-included-module-behavior/43850/3 "2008-01-18T04:16:56Z")

</div>

I would prefer to make a class, because if you intend to use include  
rather than extend in production, you ought to spec it that way. Also  
lets you take advantage of the included hook, test inheritance cases,  
etc.

klass = Class.new do  
&nbsp;&nbsp;include MyCoolModule  
end

o = klass.new

Pat

> **···**
>
> On Jan 17, 2008 12:19 PM, Judson Lester \<nyarly@gmail.com\> wrote:
> 
> > On Jan 17, 2008 10:23 AM, Starr Trader \<startrader@inbox.com\> wrote:  
> > \> Greeting all,  
> > \>  
> > \> I'm having difficulties with RSpec. I have a module that adds some  
> > \> interface functions to a class (a la Rails) and I want to test their  
> > \> functionality. (BDD and all you know)  
> > \>  
> > \> I'm not sure how to describe a module like this. It is not a class  
> > \> where I can create an instance directly for testing, I somehow need to  
> > \> create a Class that can have this module included in it.  
> > \>  
> > \> Any ideas?
> > 
> > Try something like
> > 
> > before do  
> > &nbsp;&nbsp;@test\_me = Object.new  
> > &nbsp;&nbsp;@test\_me.extend MyCoolModule  
> > end

---

<div class="post-metadata">

**Author:** ![Ben\_Mabey](https://avatars.discourse-cdn.com/v4/letter/b/ed8c4c/32.png) [@Ben\_Mabey](https://rubytalk.org/u/Ben_Mabey)\
**Post date:** [18 January 2008 04:25 UTC](https://rubytalk.org/t/using-rspec-to-describe-included-module-behavior/43850/4 "2008-01-18T04:25:55Z")

</div>

Pat Maddox wrote:

> &nbsp;&nbsp;
> 
> > > Greeting all,
> > > 
> > > I'm having difficulties with RSpec. I have a module that adds some  
> > > interface functions to a class (a la Rails) and I want to test their  
> > > functionality. (BDD and all you know)
> > > 
> > > I'm not sure how to describe a module like this. It is not a class  
> > > where I can create an instance directly for testing, I somehow need to  
> > > create a Class that can have this module included in it.
> > > 
> > > Any ideas?  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
> > 
> > Try something like
> > 
> > before do  
> > &nbsp;&nbsp;@test\_me = Object.new  
> > &nbsp;&nbsp;@test\_me.extend MyCoolModule  
> > end  
> > &nbsp;&nbsp;&nbsp;&nbsp;  
> > I would prefer to make a class, because if you intend to use include  
> > rather than extend in production, you ought to spec it that way. Also  
> > lets you take advantage of the included hook, test inheritance cases,  
> > etc.
> 
> klass = Class.new do  
> &nbsp;&nbsp;include MyCoolModule  
> end
> 
> o = klass.new
> 
> Pat

Why not just make a shared behavior and test the classes that you actually include the module in? Or did I misunderstand your question?  
(Go here: [RSpec documentation](http://rspec.info/documentation/) and search for 'Shared Examle Groups')

BTW, you should check out the rspec-users mailing list for these type of questions.

-Ben

> **···**
>
> > On Jan 17, 2008 12:19 PM, Judson Lester \<nyarly@gmail.com\> wrote:
> > 
> > > On Jan 17, 2008 10:23 AM, Starr Trader \<startrader@inbox.com\> wrote:
