Autotest giving error when having shared example in specs

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

/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.0.0.beta.20/bin/rspec
/spec/controllers/support/authorization_shared_example.rb

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:inshare_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:inload’
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:inload_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:inblock 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:inload_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:inrun_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:inblock 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 N.,
Bangalore, 560008, KA
India

Y! IM : [email protected]
GTalk : [email protected]

I too am having this error

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?

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

On Jan 21, 2011, at 4:57 AM, Rob Westgeest wrote:

Thissharedexampleis in file
getting following errors with autotest
from
`load’
/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

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

I too am having this error

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

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:inload’
/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:inload’
/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:inmap’
/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:inrun’
/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:inrun’
/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:inrequire’
/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:inrequire’
/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:inload’
/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:inload_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:inload_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:inload_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:inrun_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.

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 A.

Software Developer
[email protected]
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 [email protected]

On Jan 26, 2011, at 7:34 AM, Rob A. 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?

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?

On 26 Jan 2011, at 13:34, Rob A. wrote:

then we want to redirect to the show view instead of edit because only exams
that are draft can be edited.

47 Newton Street, Manchester, M1 1FT
[email protected]
http://rubyforge.org/mailman/listinfo/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

I believe this is caused by a bad example on

,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

On 26 Jan 2011, at 15:29, David C. wrote:

redirect_to :action => :edit

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

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