[rspec-users] shared descriptions -- a couple of issues

David Chelimsky dchelimsky at gmail.com
Thu May 10 13:27:53 EDT 2007


On 5/10/07, Greg Spurrier <greg at mydigitallife.com> wrote:
> Hello All,
>
> I've just upgraded to rspec/rspec_on_rails 0.9.3 and am very excited
> about shared descriptions.  They couldn't have come at a better time:  I
> just finished refactoring some REST controllers so that the bulk of
> the actions are inherited from a superclass.  Now I can write the
> specs once, too.  :)
>
> I've hit a couple of snags, though.  I thought I'd mention them here
> before logging bugs or attempting to fix them myself, to make sure
> that I'm not missing the point somewhere.
>
>
> Issue 1 -- multiple inclusion of shared behaviors
> =======
> I've put the shared descriptions in their own file and am requiring it
> into the files that use it.  This is no problem when I run spec on an
> individual file, but when I do 'rake spec' or run spec on a directory,
> I get the error:
>
>    Shared Behaviour 'foo' already exists (ArgumentError)
>
> It appears that there are no protections against pulling in the file
> multiple times.  I've worked around this in rails for now by naming
> the shared file XXX_shared.rb instead of XXX_spec.rb so that the rake
> task doesn't pick it up, but it seems like something that should be
> fixed.
>
> To reproduce, create the following files:
>
> a_spec.rb
> ---------
> describe "shared", :shared => true do
>    it "should do something" do
>    end
> end
>
> b_spec.rb
> ---------
> require 'a_spec'
>
> describe "not shared" do
>    it_should_behave_like "shared"
> end
>
> Now, try it out:
>
> % spec b_spec.rb
> .
>
> Finished in 0.018853 seconds
>
> 1 example, 0 failures
> % spec *_spec.rb
> /usr/local/lib/ruby/gems/1.8/gems/rspec-0.9.3/lib/spec/dsl/
> behaviour.rb:9:in `add_shared_behaviour': Shared Behaviour 'shared'
> already exists (ArgumentError)
>          from /usr/local/lib/ruby/gems/1.8/gems/rspec-0.9.3/lib/spec/
> runner/extensions/kernel.rb:16:in `register_behaviour'
>          from /usr/local/lib/ruby/gems/1.8/gems/rspec-0.9.3/lib/spec/
> runner/extensions/kernel.rb:4:in `describe'
>          from ./a_spec.rb:1
>          from /usr/local/lib/ruby/site_ruby/1.8/rubygems/
> custom_require.rb:21:in `require'
>          from ./b_spec.rb:1
>          from /usr/local/lib/ruby/gems/1.8/gems/rspec-0.9.3/lib/spec/
> runner/behaviour_runner.rb:85:in `load_specs'
>          from /usr/local/lib/ruby/gems/1.8/gems/rspec-0.9.3/lib/spec/
> runner/behaviour_runner.rb:84:in `load_specs'
>          from /usr/local/lib/ruby/gems/1.8/gems/rspec-0.9.3/lib/spec/
> runner/behaviour_runner.rb:22:in `run'
>          from /usr/local/lib/ruby/gems/1.8/gems/rspec-0.9.3/lib/spec/
> runner/command_line.rb:17:in `run'
>          from /usr/local/lib/ruby/gems/1.8/gems/rspec-0.9.3/bin/spec:3
>          from /usr/local/bin/spec:18
>
>
> Issue 2 -- Helper functions declared in shared behaviors don't work
> =======
> I've declared a helper function in the shared behavior, but it isn't
> found when the shared behavior is invoked with it_should_behave_like.
> For example:
>
> a_spec.rb
> ---------
> describe "shared", :shared => true do
>    def foobar
>      37
>    end
>
>    it "foobar should be 37" do
>      foobar.should == 37
>    end
> end
>
> describe "not shared" do
>    it_should_behave_like "shared"
> end
>
>
> yields:
>
> % spec a_spec.rb
> F
>
> 1)
> NameError in 'not shared foobar should be 37'
> undefined local variable or method `foobar' for #<#<Class:0x106555c>:
> 0x1065188>
> ./a_spec.rb:7:
>
> Finished in 0.006112 seconds
>
> 1 example, 1 failure
>
> In order to get this to work, I have to declare the foobar method in
> the non-shared description.  I imagine I can work around this by
> putting that method in a module and doing something like:
>
> describe "non-shared" do
>    include SharedHelpers
>    it_should_behave_like "shared"
> end
>
> but it's be much better if it would work as I'd expected it to work in
> the failing example.
>
>
> Thoughts?  Comments?  Am I approaching this the wrong way, or is it
> that shared descriptions are still a little rough around the edges?

You've got it right. Shared descriptions are brand new, so yes, still
rough around the edges. Please submit RFEs for both issues (separate
please).

Thanks,
David

>
> Thanks,
> Greg
>
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>


More information about the rspec-users mailing list