[rspec-users] Can't put shared example group in its own file

David Chelimsky dchelimsky at gmail.com
Tue Mar 29 00:44:42 EDT 2011


On Mar 28, 2011, at 9:29 PM, Mike Tsao wrote:

> Any ideas? All I'd like to do is have two specs in different files use
> the same shared example group. I saw a similar thread about autotest,
> but I am not using that. [Sorry for the double post on Google Groups;
> didn't realize at the time it was a read-only group.]
> 
> $ ruby -v
> ruby 1.8.7 (2009-06-12 patchlevel 174) [universal-darwin10.0]
> $ rails -v
> Rails 3.0.5
> $ rails new shared
> $ cd shared/
> $ echo "gem 'rspec-rails', '~> 2.5'" >> Gemfile
> $ bundle --without production
> $ rails generate rspec:install
> $ rails generate scaffold foo
> $ rake db:migrate
> $ mkdir spec/support
> $ nano spec/support/stuff_spec.rb

Don't name this file ^^ ending with _spec.rb - that is the default pattern that RSpec looks for specs in, so it's getting loaded as a spec. Name it something like shared_examples_for_stuff.rb.

HTH,
David

> 
> -- spec/support/stuff_spec.rb
> shared_examples_for "bar" do
> end
> 
> $ rake spec
> (in /tmp/shared)
> /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby -S
> bundle exec rspec ./spec/controllers/foos_controller_spec.rb
> ./spec/helpers/foos_helper_spec.rb ./spec/models/foo_spec.rb
> ./spec/requests/foos_spec.rb ./spec/routing/foos_routing_spec.rb
> ./spec/support/stuff_spec.rb ./spec/views/foos/edit.html.erb_spec.rb
> ./spec/views/foos/index.html.erb_spec.rb
> ./spec/views/foos/new.html.erb_spec.rb
> ./spec/views/foos/show.html.erb_spec.rb
> /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/shared_example_group.rb:45:in
> `ensure_shared_example_group_name_not_taken': Shared example group
> 'bar' already exists (ArgumentError)
>        from /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/shared_example_group.rb:6:in
> `shared_examples_for'
>        from /private/tmp/shared/spec/support/stuff_spec.rb:1
>        from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:235:in
> `load'
>        from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:235:in
> `load'
>        from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:227:in
> `load_dependency'
>        from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:235:in
> `load'
>        from /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/configuration.rb:386:in
> `load_spec_files'
>        from /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/configuration.rb:386:in
> `map'
>        from /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/configuration.rb:386:in
> `load_spec_files'
>        from /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/command_line.rb:18:in
> `run'
>        from /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/runner.rb:55:in
> `run_in_process'
>        from /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/runner.rb:46:in
> `run'
>        from /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/runner.rb:10:in
> `autorun'
>        from /Library/Ruby/Gems/1.8/bin/rspec:19
> rake aborted!


More information about the rspec-users mailing list