[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Using RSpec to Describe Included Module Behavior

Starr Trader

1/17/2008 6:24:00 PM

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-....

3 Answers

Judson Lester

1/17/2008 8:19:00 PM

0

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
@test_me = Object.new
@test_me.extend MyCoolModule
end

--
Your subnet is currently 169.254.0.0/16. You are likely to be eaten by a grue.

Pat Maddox

1/18/2008 4:17:00 AM

0

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
> @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

Ben Mabey

1/18/2008 4:26:00 AM

0

Pat Maddox wrote:
> 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
>> @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
>
>
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: http://rspec.info/docu... and search for 'Shared Examle
Groups')

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

-Ben