Amiruddin Nagri
2010-Sep-29 06:42 UTC
[rspec-users] 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 <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_nagri at yahoo.com GTalk : amir.nagri at gmail.com -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20100929/8fc0b5db/attachment.html>
LesFreeman
2010-Nov-22 20:42 UTC
[rspec-users] autotest giving error when having shared example in specs
I too am having this error On Sep 28, 10:42?pm, Amiruddin Nagri <amir.na... at 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... at yahoo.com > GTalk : amir.na... at gmail.com > > _______________________________________________ > rspec-users mailing list > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users
Andrea Reginato
2011-Jan-11 12:51 UTC
[rspec-users] autotest giving error when having shared example in specs
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
2011-Jan-21 10:57 UTC
[rspec-users] autotest giving error when having shared example in specs
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... at gmail.com> wrote:> I too am having this error > > On Sep 28, 10:42?pm, Amiruddin Nagri <amir.na... at 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 > > 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'':Sharedexamplegroup''allow > > authorized actions''alreadyexists(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 includesharedexamplefiles 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... at yahoo.com > > GTalk : amir.na... at gmail.com > > > _______________________________________________ > > rspec-users mailing list > > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users > > _______________________________________________ > rspec-users mailing list > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users
David Chelimsky
2011-Jan-21 13:35 UTC
[rspec-users] autotest giving error when having shared example in specs
On Jan 21, 2011, at 4:57 AM, Rob Westgeest wrote:> On Nov 22 2010, 9:42 pm, LesFreeman <lesliefreem... at gmail.com> wrote: >> On Sep 28, 10:42 pm, Amiruddin Nagri <amir.na... at 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 >>> 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'':Sharedexamplegroup''allow >>> authorized actions''alreadyexists(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 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/**/*.specClose, 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
2011-Jan-21 15:30 UTC
[rspec-users] autotest giving error when having shared example in specs
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... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users
Rob Aldred
2011-Jan-26 13:34 UTC
[rspec-users] Ability to stub a method differently the second time its called
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 rob at 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 info at stardotstar.com
Tom Stuart
2011-Jan-26 14:12 UTC
[rspec-users] Ability to stub a method differently the second time its called
On 26 Jan 2011, at 13:34, 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) > > 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 > rob at 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 info at stardotstar.com > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-usersHi 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
2011-Jan-26 15:29 UTC
[rspec-users] Ability to stub a method differently the second time its called
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 myselfMy 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
2011-Jan-31 12:39 UTC
[rspec-users] Ability to stub a method differently the second time its called
On 26 Jan 2011, at 15:29, David Chelimsky wrote:> 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 > endPublished (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
2011-Feb-04 05:50 UTC
[rspec-users] autotest giving error when having shared example in specs
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 On Jan 21, 5:35?am, David Chelimsky <dchelim... at gmail.com> wrote:> On Jan 21, 2011, at 4:57 AM, Rob Westgeest wrote: > > > > > > > On Nov 22 2010, 9:42 pm, LesFreeman <lesliefreem... at gmail.com> wrote: > >> On Sep 28, 10:42 pm, Amiruddin Nagri <amir.na... at 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 > >>> 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'':Sharedexamplegroup''allow > >>> authorized actions''alreadyexists(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 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. 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... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users