Using RSpec to Describe Included Module Behavior

Discussion in 'Ruby' started by Starr Trader, Jan 17, 2008.

  1. Starr Trader

    Starr Trader Guest

    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/.
     
    Starr Trader, Jan 17, 2008
    #1
    1. Advertising

  2. On Jan 17, 2008 10:23 AM, Starr Trader <> 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.
     
    Judson Lester, Jan 17, 2008
    #2
    1. Advertising

  3. Starr Trader

    Pat Maddox Guest

    On Jan 17, 2008 12:19 PM, Judson Lester <> wrote:
    >
    > On Jan 17, 2008 10:23 AM, Starr Trader <> 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
     
    Pat Maddox, Jan 18, 2008
    #3
  4. Starr Trader

    Ben Mabey Guest

    Pat Maddox wrote:
    > On Jan 17, 2008 12:19 PM, Judson Lester <> wrote:
    >
    >> On Jan 17, 2008 10:23 AM, Starr Trader <> 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/documentation/ and search for 'Shared Examle
    Groups')

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

    -Ben
     
    Ben Mabey, Jan 18, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    128
    David Chelimsky
    Feb 5, 2007
  2. Prince Nez
    Replies:
    3
    Views:
    249
    Robert Klemme
    Aug 18, 2009
  3. David Chelimsky
    Replies:
    0
    Views:
    115
    David Chelimsky
    Sep 15, 2009
  4. David Chelimsky
    Replies:
    0
    Views:
    113
    David Chelimsky
    Oct 3, 2010
  5. David Chelimsky
    Replies:
    0
    Views:
    108
    David Chelimsky
    Oct 10, 2010
Loading...

Share This Page