[rspec-users] autotest giving error when having shared example in specs

47 views
Skip to first unread message

Amiruddin Nagri

unread,
Sep 29, 2010, 2:42:26 AM9/29/10
to rspec-users
I am having a shared example 'allow authorized actions' for my Rails 3 RSpec 2.beta.20 application. The shared example looks like

share_examples_for 'allow authorized actions' do
  ...
end

This shared example is in file spec/controllers/support/authorization_shared_example.rb, which I am requiring in spec_helper.rb. I am also using autotest to give me quick feedback. Autotest is generating following command to run rspec

<path to ruby>/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.0.0.beta.20/bin/rspec <path to rails root>/spec/controllers/support/authorization_shared_example.rb <other spec files>

Since autotest is also including support file when running rspec, I am getting following errors with autotest

/Users/arusarh/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.0.0.beta.20/lib/rspec/core/shared_example_group.rb:43:in `ensure_shared_example_group_name_not_taken': Shared example group 'allow authorized actions' already exists (ArgumentError)
    from /Users/arusarh/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.0.0.beta.20/lib/rspec/core/shared_example_group.rb:6:in `share_examples_for'
    from /Users/arusarh/work/ece/carbonomist/spec/controllers/support/authorization_shared_example.rb:1:in `<top (required)>'
    from /Users/arusarh/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.0/lib/active_support/dependencies.rb:235:in `load'
    from /Users/arusarh/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.0/lib/active_support/dependencies.rb:235:in `block in load'
    from /Users/arusarh/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.0/lib/active_support/dependencies.rb:227:in `load_dependency'
    from /Users/arusarh/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.0/lib/active_support/dependencies.rb:235:in `load'
    from /Users/arusarh/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.0.0.beta.20/lib/rspec/core/configuration.rb:302:in `block in load_spec_files'
    from /Users/arusarh/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.0.0.beta.20/lib/rspec/core/configuration.rb:302:in `map'
    from /Users/arusarh/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.0.0.beta.20/lib/rspec/core/configuration.rb:302:in `load_spec_files'
    from /Users/arusarh/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.0.0.beta.20/lib/rspec/core/command_line.rb:18:in `run'
    from /Users/arusarh/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.0.0.beta.20/lib/rspec/core/runner.rb:46:in `run_in_process'
    from /Users/arusarh/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.0.0.beta.20/lib/rspec/core/runner.rb:37:in `run'
    from /Users/arusarh/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.0.0.beta.20/lib/rspec/core/runner.rb:10:in `block in autorun'


Any ideas how to not make autotest include shared example files loaded through command line, but only include files with pattern "*_spec.rb", or have anyone else facing same situation.

Regards,
Amiruddin Nagri,
Bangalore, 560008, KA
India

Y! IM : amir_...@yahoo.com
GTalk : amir....@gmail.com

LesFreeman

unread,
Nov 22, 2010, 3:42:17 PM11/22/10
to rspec...@rubyforge.org
I too am having this error

On Sep 28, 10:42 pm, Amiruddin Nagri <amir.na...@gmail.com> wrote:
> I am having a shared example 'allow authorized actions' for my Rails 3 RSpec
> 2.beta.20 application. The shared example looks like
>
> share_examples_for 'allow authorized actions' do
>   ...
> end
>
> This shared example is in file
> spec/controllers/support/authorization_shared_example.rb, which I am
> requiring in spec_helper.rb. I am also using autotest to give me quick
> feedback. Autotest is generating following command to run rspec
>
> <path to
> ruby>/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.0.0.beta.20/bin/rspec <path
> to rails root>/spec/controllers/support/authorization_shared_example.rb
> <other spec files>
>
> Since autotest is also including support file when running rspec, I am
> getting following errors with autotest
>

> /Users/arusarh/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.0.0.beta.20/lib/rs pec/core/shared_example_group.rb:43:in


> `ensure_shared_example_group_name_not_taken': Shared example group 'allow
> authorized actions' already exists (ArgumentError)
>     from
> /Users/arusarh/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.0.0.beta.20/lib/rs pec/core/shared_example_group.rb:6:in
> `share_examples_for'
>     from

> /Users/arusarh/work/ece/carbonomist/spec/controllers/support/authorization_ shared_example.rb:1:in


> `<top (required)>'
>     from
> /Users/arusarh/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.0/lib/active_ support/dependencies.rb:235:in
> `load'
>     from

> /Users/arusarh/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.0/lib/active_ support/dependencies.rb:235:in


> `block in load'
>     from
> /Users/arusarh/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.0/lib/active_ support/dependencies.rb:227:in
> `load_dependency'
>     from
> /Users/arusarh/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.0/lib/active_ support/dependencies.rb:235:in
> `load'
>     from

> /Users/arusarh/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.0.0.beta.20/lib/rs pec/core/configuration.rb:302:in


> `block in load_spec_files'
>     from
> /Users/arusarh/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.0.0.beta.20/lib/rs pec/core/configuration.rb:302:in
> `map'
>     from
> /Users/arusarh/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.0.0.beta.20/lib/rs pec/core/configuration.rb:302:in
> `load_spec_files'
>     from
> /Users/arusarh/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.0.0.beta.20/lib/rs pec/core/command_line.rb:18:in
> `run'
>     from
> /Users/arusarh/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.0.0.beta.20/lib/rs pec/core/runner.rb:46:in
> `run_in_process'
>     from
> /Users/arusarh/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.0.0.beta.20/lib/rs pec/core/runner.rb:37:in
> `run'
>     from

> /Users/arusarh/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.0.0.beta.20/lib/rs pec/core/runner.rb:10:in


> `block in autorun'
>
> Any ideas how to not make autotest include shared example files loaded
> through command line, but only include files with pattern "*_spec.rb", or
> have anyone else facing same situation.
>
> Regards,
> Amiruddin Nagri,
> Bangalore, 560008, KA
> India
>

> Y! IM : amir_na...@yahoo.com
> GTalk : amir.na...@gmail.com
>
> _______________________________________________
> rspec-users mailing list
> rspec-us...@rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users
_______________________________________________
rspec-users mailing list
rspec...@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Andrea Reginato

unread,
Jan 11, 2011, 7:51:28 AM1/11/11
to rspec...@rubyforge.org
I'm sure is not the best solution, but I solved the problem creating an
Initializer monkey patching the method giving the error. I'm pretty new
on Ruby and company, as always scratching the surface, and only now
getting into the deep, but I hope it will help.

# extract config/initializers/rspec_hacks.rb
module RSpec
module Core
module SharedExampleGroup
private
def ensure_shared_example_group_name_not_taken(name)
end
end
end
end

--
Posted via http://www.ruby-forum.com/.

Rob Westgeest

unread,
Jan 21, 2011, 5:57:50 AM1/21/11
to rspec...@rubyforge.org
I am having the same problem.

Autotest with rspec seem to differ from rspecs rake task in which
files to automatically require.

The rake task requires spec/**/*.spec
Autotest rspec2 requires spec/**/*

As I am sharing example groups in their own files names
(shared_*_examples.rb), and require them from *spec.rb files, rake
works nicely and autotest fails because of its 'lets require
everything' attitude.

I presume that there is a way to configure autotests require set, but
i haven't found it yet.

Anyone?

On Nov 22 2010, 9:42 pm, LesFreeman <lesliefreem...@gmail.com> wrote:
> I too am having this error
>
> On Sep 28, 10:42 pm, Amiruddin Nagri <amir.na...@gmail.com> wrote:
>

> > I am having asharedexample'allow authorized actions' for my Rails 3 RSpec
> > 2.beta.20 application. Thesharedexamplelooks like


>
> > share_examples_for 'allow authorized actions' do
> >   ...
> > end
>

> > Thissharedexampleis in file

> > Any ideas how to not make autotest includesharedexamplefiles loaded

David Chelimsky

unread,
Jan 21, 2011, 8:35:36 AM1/21/11
to rspec-users

> I am having the same problem.


>
> Autotest with rspec seem to differ from rspecs rake task in which
> files to automatically require.
>
> The rake task requires spec/**/*.spec

Close, but not quite. The default pattern is spec/**/*_spec.rb.

> Autotest rspec2 requires spec/**/*

Again, this incorrect. Autotest fires off shell commands that include a list of files to load based on a set of mappings: if file x changes, run a command with file(s) y.

In RSpec, those mappings are defined in Autotest::Rspec2, and they are set to include files that end with _spec.rb, with some additional constraints based on file naming conventions: a change to lib/foo/bar.rb tells RSpec to include spec/foo/bar_spec.rb.

> As I am sharing example groups in their own files names
> (shared_*_examples.rb), and require them from *spec.rb files, rake
> works nicely and autotest fails because of its 'lets require
> everything' attitude.

I'm not sure where that's coming from, but it's not default settings of rspec or autotest. What versions of rspec and autotest are you using? Is this a rails app?

>
> I presume that there is a way to configure autotests require set, but
> i haven't found it yet.

Autotest has a mapping mechanism that you can access in its initialize hook. See http://blog.davidchelimsky.net/2008/01/15/rspec-1-1-2-and-zentest-3-8-0/ for more on this.

Cheers,
David

Rob Westgeest

unread,
Jan 21, 2011, 10:30:54 AM1/21/11
to rspec...@rubyforge.org
Thanks for your fast reply David,

> > The rake task requires spec/**/*.spec
>
> Close, but not quite. The default pattern is spec/**/*_spec.rb.

Typo sorry

>
> > Autotest rspec2 requires spec/**/*
>
> Again, this incorrect. Autotest fires off shell commands that include a list of files to load based on a set of mappings: if file x changes, run a command with file(s) y.
>
> In RSpec, those mappings are defined in Autotest::Rspec2, and they are set to include files that end with _spec.rb, with some additional constraints based on file naming conventions: a change to lib/foo/bar.rb tells RSpec to include spec/foo/bar_spec.rb.

Yeah i saw that in the code. I just could not figure out why files
like:

spec/models/csv/shared_export_examples.rb

end up in the 'bundle exec /usr/bin/ruby1.8 -S /var/lib/gems/1.8/gems/
rspec-core-2.4.0/bin/rspec --tty'.... command line along with all the
_spec.rb files.

anohter thing that I don't quite understand is that even if i would
require this file again, why on earth ruby decides to require it if it
already was. For the example above, if I put

puts "-------------\n" + caller.join("\n")

i get two slightly different stacktraces:

-------------
/var/lib/gems/1.8/gems/activesupport-3.0.3/lib/active_support/
dependencies.rb:235:in `load'
/var/lib/gems/1.8/gems/activesupport-3.0.3/lib/active_support/
dependencies.rb:235:in `load'
/var/lib/gems/1.8/gems/activesupport-3.0.3/lib/active_support/
dependencies.rb:227:in `load_dependency'
/var/lib/gems/1.8/gems/activesupport-3.0.3/lib/active_support/
dependencies.rb:235:in `load'
/var/lib/gems/1.8/gems/rspec-core-2.4.0/lib/rspec/core/
configuration.rb:387:in `load_spec_files'
/var/lib/gems/1.8/gems/rspec-core-2.4.0/lib/rspec/core/
configuration.rb:387:in `map'
/var/lib/gems/1.8/gems/rspec-core-2.4.0/lib/rspec/core/
configuration.rb:387:in `load_spec_files'
/var/lib/gems/1.8/gems/rspec-core-2.4.0/lib/rspec/core/command_line.rb:
18:in `run'
/var/lib/gems/1.8/gems/rspec-core-2.4.0/lib/rspec/core/runner.rb:55:in
`run_in_process'
/var/lib/gems/1.8/gems/rspec-core-2.4.0/lib/rspec/core/runner.rb:46:in
`run'
/var/lib/gems/1.8/gems/rspec-core-2.4.0/lib/rspec/core/runner.rb:10
/var/lib/gems/1.8/gems/rspec-core-2.4.0/bin/rspec:4
-------------
/var/lib/gems/1.8/gems/activesupport-3.0.3/lib/active_support/
dependencies.rb:239:in `require'
/var/lib/gems/1.8/gems/activesupport-3.0.3/lib/active_support/
dependencies.rb:239:in `require'
/var/lib/gems/1.8/gems/activesupport-3.0.3/lib/active_support/
dependencies.rb:227:in `load_dependency'
/var/lib/gems/1.8/gems/activesupport-3.0.3/lib/active_support/
dependencies.rb:239:in `require'
/home/rob/dev/ws/wc-rubyapps/tafelmanager/spec/support/
require_relative.rb:2:in `require_relative'
/home/rob/dev/ws/wc-rubyapps/tafelmanager/spec/models/csv/
person_exporter_spec.rb:2
/var/lib/gems/1.8/gems/activesupport-3.0.3/lib/active_support/
dependencies.rb:235:in `load'
/var/lib/gems/1.8/gems/activesupport-3.0.3/lib/active_support/
dependencies.rb:235:in `load'
/var/lib/gems/1.8/gems/activesupport-3.0.3/lib/active_support/
dependencies.rb:227:in `load_dependency'
/var/lib/gems/1.8/gems/activesupport-3.0.3/lib/active_support/
dependencies.rb:235:in `load'
/var/lib/gems/1.8/gems/rspec-core-2.4.0/lib/rspec/core/
configuration.rb:387:in `load_spec_files'
/var/lib/gems/1.8/gems/rspec-core-2.4.0/lib/rspec/core/
configuration.rb:387:in `map'
/var/lib/gems/1.8/gems/rspec-core-2.4.0/lib/rspec/core/
configuration.rb:387:in `load_spec_files'
/var/lib/gems/1.8/gems/rspec-core-2.4.0/lib/rspec/core/command_line.rb:
18:in `run'
/var/lib/gems/1.8/gems/rspec-core-2.4.0/lib/rspec/core/runner.rb:55:in
`run_in_process'
/var/lib/gems/1.8/gems/rspec-core-2.4.0/lib/rspec/core/runner.rb:46:in
`run'
/var/lib/gems/1.8/gems/rspec-core-2.4.0/lib/rspec/core/runner.rb:10
/var/lib/gems/1.8/gems/rspec-core-2.4.0/bin/rspec:4

btw, just in case you wondered what require_relative means:
require_relative is:

def require_relative(file, requirement)
require File.expand_path(File.join(File.dirname(file),requirement))
end

> I'm not sure where that's coming from, but it's not default settings of rspec or autotest. What versions of rspec and autotest are you using? Is this a rails app?

using:
Rails 3.0.3

bundle list yields (a.o.):
* ZenTest (4.4.2)
* autotest (4.4.6)
* autotest-rails (4.1.0)
* rspec (2.4.0)
* rspec-core (2.4.0)
* rspec-expectations (2.4.0)
* rspec-mocks (2.4.0)
* rspec-rails (2.4.1)

>
>
>
> > I presume that there is a way to configure autotests require set, but
> > i haven't found it yet.
>
> Autotest has a mapping mechanism that you can access in its initialize hook. Seehttp://blog.davidchelimsky.net/2008/01/15/rspec-1-1-2-and-zentest-3-8-0/for more on this.

Thanks - i'll try to add an exception for the shared.*examples.rb
files for now, while looking for the root cause these duplicated
requires.

>
> Cheers,
> David
> _______________________________________________
> rspec-users mailing list

> rspec-us...@rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users

Rob Aldred

unread,
Jan 26, 2011, 8:34:35 AM1/26/11
to rspec-users
I'm having a bit of trouble stubbing out a method on a model which has some quite specific behaviour.
Basically I want to check the method returns something different after ive called update_attributes

eg.

@exam.draft? (returns true)
@exam.update_attributes(params[:exam]) # sets draft to false
@exam.draft? (returns false)

To give this some context, if the exam is a draft and update_attributes sets draft to false
then we want to redirect to the show view instead of edit because only exams that are draft can be edited.

if @exam.draft?
@exam.update_attributes param[:exam]
end

if @exam.draft?
redirect_to :action => :edit
else
redirect_to :action => :show
end

I can obviously set consecutive values with and_return(true,false)
but this feels a bit unreliable? maybe im just confusing myself

--
Rob Aldred

Software Developer
r...@stardotstar.com
twitter: stardotstar

47 Newton Street, Manchester, M1 1FT
T: +44 (0) 161 236 9740
___________________________________________________

This email and any files or ideas transmitted within it are sent in
confidence and are intended solely for the use of the individual or
entity to whom they are addressed. If you have received this email
in error please notify the system manager at in...@stardotstar.com

Tom Stuart

unread,
Jan 26, 2011, 9:12:41 AM1/26/11
to rspec-users

Hi Rob,

It sounds to me like you're trying to test two different behaviours in one 'it' block, something like:

it "redirects based on whether the exam is a draft" do
@exam.stub!(:draft).and_return(true, false)

#make request, assert response redirected to edit
#make request again, assert reponse redirected to edit
end

To describe your behaviour more clearly, you might want to consider splitting this test and using contexts, something along the lines of...

describe "your action" do
context "when exam is a draft" do
@exam.stub!(:draft).and_return(true)

it "redirects to edit" do
end
end

context "when exam is final" do
@exam.stub!(:draft).and_return(false)

it "redirects to "show" do
end
end
end

Which gives you the benefit of making what you need to stub obvious based on the context.

Hope that helps,

Tom

David Chelimsky

unread,
Jan 26, 2011, 10:29:48 AM1/26/11
to rspec-users
On Jan 26, 2011, at 7:34 AM, Rob Aldred wrote:

> I'm having a bit of trouble stubbing out a method on a model which has some quite specific behaviour.
> Basically I want to check the method returns something different after ive called update_attributes
>
> eg.
>
> @exam.draft? (returns true)
> @exam.update_attributes(params[:exam]) # sets draft to false
> @exam.draft? (returns false)

You're testing the framework here. Why?

> To give this some context, if the exam is a draft and update_attributes sets draft to false
> then we want to redirect to the show view instead of edit because only exams that are draft can be edited.
>
> if @exam.draft?
> @exam.update_attributes param[:exam]
> end
>
> if @exam.draft?
> redirect_to :action => :edit
> else
> redirect_to :action => :show
> end
>
> I can obviously set consecutive values with and_return(true,false)
> but this feels a bit unreliable? maybe im just confusing myself

My first question is why the update_attributes needs to be conditional? Couldn't you just do:

@exam.update_attributes param[:exam]

if @exam.draft?
redirect_to :action => :edit
else
redirect_to :action => :show
end

If it must be the way you have it, I wouldn't do this with stubs on the @exam itself. I'd do what you need to do to set the exam in a draft state (Factory(:exam, :draft => true)???), and then invoke the controller action with :draft => true in one example, and :draft => false in the other.

That make sense?

Rob Aldred

unread,
Jan 31, 2011, 7:39:58 AM1/31/11
to rspec-users

Published (not draft) exams are not allowed to be edited

We are using couchDB the couchrest_model gem does not implement ActiveModel::Dirty
so there is no way to check for changing values eg... draft_was or draft_changed? so implementing a validator in the model is a pain without making another call to the db to check the current draft status before_save

The reason for the double conditional because if the user passes the draft param set to false (To make the exam published) we need to allow them to save the instance.

We then use the 2nd conditional to decide where to send them based on the final state of the exam instance
whether draft was changed or not.

>
> If it must be the way you have it, I wouldn't do this with stubs on the @exam itself. I'd do what you need to do to set the exam in a draft state (Factory(:exam, :draft => true)???), and then invoke the controller action with :draft => true in one example, and :draft => false in the other.
>
> That make sense?
>
>

Yes, this would work and your right I guess I was testing the framework.
This seems like a workable solution.

Thanks for your time.
Rob

Adam Greene

unread,
Feb 4, 2011, 12:50:15 AM2/4/11
to rspec...@rubyforge.org
I believe this is caused by a bad example on https://github.com/rspec/rspec/wiki/autotest
,specifically under the section "Advanced autotest configuration".

I ran into this exact same issue and it was solved by changing the
example to use spec/**/*_spec.rb.

I went ahead and took the liberty of updating the wiki page

thanks,
adam

> Autotest has a mapping mechanism that you can access in its initialize hook. Seehttp://blog.davidchelimsky.net/2008/01/15/rspec-1-1-2-and-zentest-3-8-0/for more on this.


>
> Cheers,
> David
> _______________________________________________
> rspec-users mailing list

> rspec-us...@rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users

Reply all
Reply to author
Forward
0 new messages