# Using RSpec to Describe Included Module Behavior

**URL:** <https://www.ruby-forum.com/t/using-rspec-to-describe-included-module-behavior/126363>\
**Category:** Ruby\
**Created:** [January 17, 2008, 7:23pm UTC](https://www.ruby-forum.com/t/using-rspec-to-describe-included-module-behavior/126363 "2008-01-17T19:23:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![startrader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a6a055/32.png) [@startrader](https://www.ruby-forum.com/u/startrader)\
**Post date:** [January 17, 2008, 7:23pm UTC](https://www.ruby-forum.com/t/using-rspec-to-describe-included-module-behavior/126363/1 "2008-01-17T19:23:29Z")

</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?

---

<div class="post-metadata">

**Author:** ![startrader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a6a055/32.png) [@startrader](https://www.ruby-forum.com/u/startrader)\
**Post date:** [January 17, 2008, 9:20pm UTC](https://www.ruby-forum.com/t/using-rspec-to-describe-included-module-behavior/126363/2 "2008-01-17T21:20:25Z")

</div>

On Jan 17, 2008 10:23 AM, Starr T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Any ideas?  
> Try something like

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

---

<div class="post-metadata">

**Author:** ![startrader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a6a055/32.png) [@startrader](https://www.ruby-forum.com/u/startrader)\
**Post date:** [January 18, 2008, 5:18am UTC](https://www.ruby-forum.com/t/using-rspec-to-describe-included-module-behavior/126363/3 "2008-01-18T05:18:24Z")

</div>

On Jan 17, 2008 12:19 PM, Judson L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > create a Class that can have this module included in it.
> > 
> > Any ideas?
> 
> Try something like
> 
> before do  
> @test\_me = Object.new  
> @test\_me.extend MyCoolModule  
> end

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  
include MyCoolModule  
end

o = klass.new

Pat

---

<div class="post-metadata">

**Author:** ![startrader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a6a055/32.png) [@startrader](https://www.ruby-forum.com/u/startrader)\
**Post date:** [January 18, 2008, 5:26am UTC](https://www.ruby-forum.com/t/using-rspec-to-describe-included-module-behavior/126363/4 "2008-01-18T05:26:35Z")

</div>

Pat M. wrote:

> > > I’m not sure how to describe a module like this. It is not a class  
> > > 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
