Forum: RSpec Best way to spec nested modules?...

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Df31a543d684be5fd6bc6c188d378615?d=identicon&s=25 Stuart Hungerford (Guest)
on 2009-01-26 04:44
(Received via mailing list)
Hi,

I've got a set of classes in nested Ruby modules which I'm using rspec
to specify:

module Foo

     module Baz

        class C1 ... end

        class C2 ... end
     end
end

To specify C2 behaviour I need to create a bunch of C1 instances:

describe Foo::Baz::C2 do

     before(:each) do
         @c1a = Foo::Baz::C1.new(...)
         @c1b = Foo::Baz::C1.new(...)
         @c1c = Foo::Baz::C1.new(...)

         @c2 = Foo::Baz::C2.new(@c1a, @c1b, @c1c)
     end

     describe "some behaviour" do
         # ...
     end
end

After a while the many Foo::Baz:: module prefixes become pretty
tedious and
not particularly DRY.

Can someone suggest a better way to manage using nested modules in
rspec?

Thanks,

Stu

--
Stuart Hungerford
ANU Supercomputer Facility
42172acdf3c6046f84d644cb0b94642c?d=identicon&s=25 Pat Maddox (pergesu)
on 2009-01-26 04:58
(Received via mailing list)
Put the describe in the module, and take advantage of Ruby's lookup
semantics:

module Foo
  module Baz
    describe Class1 do

...

Pat
49de4cd2f26705785cbef2b15a9df7aa?d=identicon&s=25 Nick Hoffman (nickh)
on 2009-01-26 19:16
(Received via mailing list)
On 25/01/2009, at 9:56 PM, Stuart Hungerford wrote:
>
>        @c1b = Foo::Baz::C1.new(...)
> After a while the many Foo::Baz:: module prefixes become pretty
> --
> Stuart Hungerford
> ANU Supercomputer Facility

Hey Stuart. You should also consider using mocks and stubs. Eg:

module Foo
   module Baz
     before :each do
       @c1a = mock_model C1, ...
       @c1b = mock_model C1, ...
       @c1c = mock_model C1, ...

       @c2 = C2.new @c1a, @c1b, @c1c
     end
   end
end

That way, your specs for class C2 aren't tied to C1's implementation.

One other suggestion I'd make is to use more descriptive variable
names, though "c1a" might simply be for example's sake, since we're
talking about Foo::Baz::C1, etc.

Cheers,
Nick
This topic is locked and can not be replied to.