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
Put the describe in the module, and take advantage of Ruby''s lookup
semantics:
module Foo
module Baz
describe Class1 do
...
Pat
On 1/25/09, Stuart Hungerford <stuart.hungerford at anu.edu.au>
wrote:> 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
>
>
>
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>
On 25/01/2009, at 9:56 PM, Stuart Hungerford wrote:> 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 FacilityHey 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