From dchelimsky at gmail.com Sat Jul 2 12:35:49 2011 From: dchelimsky at gmail.com (David Chelimsky) Date: Sat, 2 Jul 2011 11:35:49 -0500 Subject: [rspec-users] Collection membership Error In-Reply-To: <636275ae-3cf9-4861-9323-01746dd43e8d@r2g2000vbj.googlegroups.com> References: <636275ae-3cf9-4861-9323-01746dd43e8d@r2g2000vbj.googlegroups.com> Message-ID: On Thu, Jun 30, 2011 at 6:29 PM, Alex Katebi wrote: > > Using rspec version 2.6.4 I get an error with the Collection > membership. > > Example: [1,2].should include(1) The `include` method needs to be in an example for you to use it like this. It won't work in irb unless you first `include RSpec::Matchers`. > > TypeError: wrong argument type Fixnum (expected Module) > ? ? ? ?from (irb):1:in `include' > ? ? ? ?from (irb):1 > ? ? ? ?from /home/akatebi/.rvm/rubies/ruby-1.9.2-p180/bin/irb:16:in `
' > > > RubyGems Environment: > ?- RUBYGEMS VERSION: 1.6.2 > ?- RUBY VERSION: 1.9.2 (2011-02-18 patchlevel 180) [i686-linux] > ?- INSTALLATION DIRECTORY: /home/akatebi/.rvm/gems/ruby-1.9.2- > p180 at endpoints > ?- RUBY EXECUTABLE: /home/akatebi/.rvm/rubies/ruby-1.9.2-p180/bin/ > ruby > ?- EXECUTABLE DIRECTORY: /home/akatebi/.rvm/gems/ruby-1.9.2- > p180 at endpoints/bin > ?- RUBYGEMS PLATFORMS: > ? ?- ruby > ? ?- x86-linux > ?- GEM PATHS: > ? ? - /home/akatebi/.rvm/gems/ruby-1.9.2-p180 at endpoints > ? ? - /home/akatebi/.rvm/gems/ruby-1.9.2-p180 at global > ?- GEM CONFIGURATION: > ? ? - :update_sources => true > ? ? - :verbose => true > ? ? - :benchmark => false > ? ? - :backtrace => false > ? ? - :bulk_threshold => 1000 > ?- REMOTE SOURCES: > ? ? - http://rubygems.org/ > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > From john.hinnegan at gmail.com Sat Jul 2 21:53:54 2011 From: john.hinnegan at gmail.com (John Hinnegan) Date: Sat, 2 Jul 2011 18:53:54 -0700 Subject: [rspec-users] can't access route variables Message-ID: I'm testing methods in my ApplicationController using anonymous controller class (not sure if that's relevant). I've somehow broken something such that I do not have access to the routing variables that rails generates. So rather than having something like 'response.should redirect_to new_user_session_path' I'm having to go with 'response.should redirect_to '/users/sign_in' I've created a new project to check if I messed anything in spec_helper or something else, and I've been unable to reproduce outside of my current project. I can access the route variables from other controller spec classes, but not from the application_controller_spec. Can anyone point me at where to look to start troubleshooting this? Rails 3.0.5 $ gem list | grep rspec rspec (2.5.0) rspec-core (2.5.2) rspec-expectations (2.5.0) rspec-mocks (2.5.0) rspec-rails (2.5.0) Code snippets /app/controllers/application_controller.rb class ApplicationController < ActionController::Base protect_from_forgery def require_bar if !user_signed_in? redirect_to new_user_session_path return end # check for bar here end end /spec/controllers/application_controller_spec.rb require 'spec_helper' describe ApplicationController do describe "#require_bar" do controller do before_filter :require_bar def index render :text => "", :status => 200 end end context "when not signed in" do before :each do controller.stub(:user_signed_in?).and_return(false) end it "should fail gracefully by redirecting to log in" do get :index # This works response.should redirect_to '/users/sign_in' # this causes: undefined local variable or method `new_merchant_session_path' for # # response.should redirect_to new_user_session_path end end end -------------- next part -------------- An HTML attachment was scrubbed... URL: From john.hinnegan at gmail.com Sun Jul 3 02:08:23 2011 From: john.hinnegan at gmail.com (John Hinnegan) Date: Sat, 2 Jul 2011 23:08:23 -0700 Subject: [rspec-users] Caching in Rails by Default Message-ID: So, maybe I need to go to the rails list with this, but I hit my error while testing so I figured I'd ask here first. Basically, Rails.cache.fetch is being persisted by default in my rails project between rspec runs. I've illustrated what I'm observing here: https://github.com/softwaregravy/rspec-troubleshoot/commit/c63b4b09fa6e70c2312e0aa2f7ba0ce078322ccc Furthermore, if you run that once, and then take out the test that doesn't raise the error, its still in the cache. So, to get at the 'issue' I'm describing specifically. If you first run 'rspec spec/controllers/application_controller_spec.rb' with the following: https://github.com/softwaregravy/rspec-troubleshoot/blob/0e22ce887cde602e49dc9bf7fa25406882b038f0/spec/controllers/application_controller_spec.rb you seed the cache. And then run it again without the happy case: https://github.com/softwaregravy/rspec-troubleshoot/blob/1e36222e64bfd9c473be06fba0a5fa102d4bf0f6/spec/controllers/application_controller_spec.rb You continue to get the failing test because the results of that block are cached. Now, I'm not that familiar with Rails caching. But it appears that there might be an issue in which Rails is not respecting the perform_caching flag. If that's not the root cause, then it seems to be that rspec could be doing a Rails.cache.clear and/or overriding a memory cache to be used during tests. Thoughts? *** LOCAL GEMS *** abstract (1.0.0) actionmailer (3.0.9, 3.0.5) actionpack (3.0.9, 3.0.5) activemodel (3.0.9, 3.0.5) activerecord (3.0.9, 3.0.5) activeresource (3.0.9, 3.0.5) activesupport (3.0.9, 3.0.5) arel (2.0.10) builder (2.1.2) bundler (1.0.15) diff-lcs (1.1.2) erubis (2.6.6) i18n (0.6.0, 0.5.0) mail (2.2.19) mime-types (1.16) polyglot (0.3.1) rack (1.2.3) rack-mount (0.6.14) rack-test (0.5.7) rails (3.0.9, 3.0.5) railties (3.0.9, 3.0.5) rake (0.8.7) rdoc (3.8) rspec (2.6.0) rspec-core (2.6.4) rspec-expectations (2.6.0) rspec-mocks (2.6.0) rspec-rails (2.6.1) sqlite3 (1.3.3) thor (0.14.6) treetop (1.4.9) tzinfo (0.3.29) ruby-1.9.2-p180 at rspec-troubleshoot: system: uname: "Darwin John-Hinnegans-MacBook-Pro.local 10.8.0 Darwin Kernel Version 10.8.0: Tue Jun 7 16:33:36 PDT 2011; root:xnu-1504.15.3~1/RELEASE_I386 i386" bash: "/bin/bash => GNU bash, version 3.2.48(1)-release (x86_64-apple-darwin10.0)" zsh: "/bin/zsh => zsh 4.3.9 (i386-apple-darwin10.0)" rvm: version: "rvm 1.2.8 by Wayne E. Seguin (wayneeseguin at gmail.com) [ http://rvm.beginrescueend.com/]" ruby: interpreter: "ruby" version: "1.9.2p180" date: "2011-02-18" platform: "x86_64-darwin10.6.0" patchlevel: "2011-02-18 revision 30909" full_version: "ruby 1.9.2p180 (2011-02-18 revision 30909) [x86_64-darwin10.6.0]" -------------- next part -------------- An HTML attachment was scrubbed... URL: From mmazur at gmail.com Sun Jul 3 03:32:20 2011 From: mmazur at gmail.com (Mike Mazur) Date: Sun, 3 Jul 2011 15:32:20 +0800 Subject: [rspec-users] can't access route variables In-Reply-To: References: Message-ID: Hi, On Sun, Jul 3, 2011 at 09:53, John Hinnegan wrote: > I'm testing methods in my ApplicationController using anonymous controller > class (not sure if that's relevant).? I've somehow broken something such > that I do not have access to the routing variables that rails generates. > > So rather than having something like 'response.should redirect_to > new_user_session_path' I'm having to go with 'response.should redirect_to > '/users/sign_in' > > I've created a new project to check if I messed anything in spec_helper or > something else, and I've been unable to reproduce outside of my current > project.? I can access the route variables from other controller spec > classes, but not from the application_controller_spec. > > Can anyone point me at where to look to start troubleshooting this? > > Rails 3.0.5 > $ gem list | grep rspec > rspec (2.5.0) > rspec-core (2.5.2) > rspec-expectations (2.5.0) > rspec-mocks (2.5.0) > rspec-rails (2.5.0) > > > Code snippets > /app/controllers/application_controller.rb > class ApplicationController < ActionController::Base > ? protect_from_forgery > > ? def require_bar > ??? if > !user_signed_in? > ????? redirect_to new_user_session_path > ????? return > ??? end > ?? # check for bar here > ? end > end > > /spec/controllers/application_controller_spec.rb > require 'spec_helper' > > describe ApplicationController do > ? describe "#require_bar" do > ??? controller do > ????? before_filter :require_bar > ????? def index > ??????? render :text => "", :status => 200 > ????? end > ??? end > > ??? context "when not signed in" do > ????? before :each do > ??????? controller.stub(:user_signed_in?).and_return(false) > ????? end > ????? it "should fail gracefully by redirecting to log in" do > ??????? get :index > ??????? # This works > ??????? response.should redirect_to '/users/sign_in' > ??????? # this causes: undefined local variable or method > `new_merchant_session_path' for > # > ??????? # response.should redirect_to new_user_session_path > ????? end > ??? end > ? end RSpec's anonymous controller does not use your routes. If you look at how the controller method is implemented[1], you'll see the routes being replaced with just the anonymous resources. You can do something similar in a before block in your spec. For example: describe ApplicationController do ? describe "#require_bar" do ??? controller do ????? before_filter :require_bar ????? def index ??????? render :text => "", :status => 200 ????? end ??? end before do @routes.draw do resources :anonymous # for all other specs to keep working resources :user_sessions # this makes new_user_session_path available end end ??? context "when not signed in" do ????? before :each do ??????? controller.stub(:user_signed_in?).and_return(false) ????? end ????? it "should fail gracefully by redirecting to log in" do ??????? get :index ??????? response.should redirect_to new_user_session_path ????? end ??? end ? end end Hope that helps, Mike [1] https://github.com/rspec/rspec-rails/blob/master/lib/rspec/rails/example/controller_example_group.rb#L127 From dchelimsky at gmail.com Sun Jul 3 05:45:03 2011 From: dchelimsky at gmail.com (David Chelimsky) Date: Sun, 3 Jul 2011 04:45:03 -0500 Subject: [rspec-users] Caching in Rails by Default In-Reply-To: References: Message-ID: <9CDD3607-E80D-4564-8E08-2173D97B5717@gmail.com> On Jul 3, 2011, at 1:08 AM, John Hinnegan wrote: > So, maybe I need to go to the rails list with this, but I hit my error while testing so I figured I'd ask here first. > > Basically, Rails.cache.fetch is being persisted by default in my rails project between rspec runs. I've illustrated what I'm observing here: > > https://github.com/softwaregravy/rspec-troubleshoot/commit/c63b4b09fa6e70c2312e0aa2f7ba0ce078322ccc > > Furthermore, if you run that once, and then take out the test that doesn't raise the error, its still in the cache. > > So, to get at the 'issue' I'm describing specifically. If you first run 'rspec spec/controllers/application_controller_spec.rb' with the following: > https://github.com/softwaregravy/rspec-troubleshoot/blob/0e22ce887cde602e49dc9bf7fa25406882b038f0/spec/controllers/application_controller_spec.rb > you seed the cache. And then run it again without the happy case: > https://github.com/softwaregravy/rspec-troubleshoot/blob/1e36222e64bfd9c473be06fba0a5fa102d4bf0f6/spec/controllers/application_controller_spec.rb > > You continue to get the failing test because the results of that block are cached. > > Now, I'm not that familiar with Rails caching. But it appears that there might be an issue in which Rails is not respecting the perform_caching flag. Looks like the perform_caching config option only impacts page caching. See http://pivotallabs.com/users/spierson/blog/articles/818-standup-04-30-2009-perform-caching-and-rails-cache-w3c-dtds-and-ie-range-min-max for a brief discussion of options (that's the first hit in a google search for "perform_caching", btw). > If that's not the root cause, then it seems to be that rspec could be doing a Rails.cache.clear and/or overriding a memory cache to be used during tests. You can definitely do this yourself in a global before hook, but I don't know how people use Rails.cache in the wild and wouldn't want to do anything implicit that might break existing suites. I'd recommend using a global before hook or raising an issue in the rails tracker. HTH, David -------------- next part -------------- An HTML attachment was scrubbed... URL: From itsterry at gmail.com Thu Jul 7 20:12:43 2011 From: itsterry at gmail.com (itsterry) Date: Thu, 7 Jul 2011 17:12:43 -0700 (PDT) Subject: [rspec-users] Rspec2: checking a view calls a helper Message-ID: <83f6c4cd-dd83-49f4-922c-237a6a668040@gc3g2000vbb.googlegroups.com> I have a view which calls a helper. Lets' call it "abc" So in my view, I write =abc #(I'm using haml) When I run the view in my webserver, all works fine But when I run my view spec, it tells me undefined local variable or method 'abc' for #<#Class:0xnumbers> I've googled this, but can't find the right way to tell the spec to access the helper. Any clues, anyone? If it helps, here's (an edited version of) the view spec code: describe "users/index.html.haml" do before do render end it "stuff" do rendered.should have_selector('whatever') end end TIA for any help T From paragd86 at gmail.com Tue Jul 5 16:46:24 2011 From: paragd86 at gmail.com (Parag Dave) Date: Tue, 5 Jul 2011 13:46:24 -0700 (PDT) Subject: [rspec-users] Help with the rspec and the xpath Message-ID: Hi Geeks, i need help with the rspec, Some time i having a problem with locating the element with the xpath Same element, i can able to select some time where some time i can not. Is there a bug or i need to do some specific for that? My way to locate xpath browser.link(:xpath,"/html/body/div[@id='page']/div[3]/ div[@id='right']/div[@id='control-panel']/ul/li[12]/a/img").click browser.checkbox(:xpath,"/html/body/div[@id='page']/div[3]/ div[@id='right']/form[@id='ModulePreferenceSettingsModulesForm']/table/ tbody/tr[3]/td[2]/div/input[@id='ModulePreference6Enable']").clear I use firefox addon Xpather that create xpath of the HTML elements. I appreciate all reply please give guidance. From chandu.shenoy at gmail.com Fri Jul 8 02:59:36 2011 From: chandu.shenoy at gmail.com (Chandu80) Date: Thu, 7 Jul 2011 23:59:36 -0700 (PDT) Subject: [rspec-users] Rspec or Test:Unit Message-ID: <130dda5b-aad5-4127-9396-509c9c1b9baf@n5g2000yqh.googlegroups.com> Hello All, I want to know which out of Rspec and Test:Unit is the most used in the industry for unit/integration testing and why? Regards Chandrika From genlighten at gmail.com Fri Jul 8 17:37:05 2011 From: genlighten at gmail.com (Dean) Date: Fri, 8 Jul 2011 14:37:05 -0700 (PDT) Subject: [rspec-users] Trouble with RSpec mock "@macropost.should_receive(:save).and_return(true)" -- produces error expected :save with (any args) once, but received it 0 times Message-ID: <2826788.299.1310161025722.JavaMail.geo-discussion-forums@yqcj24> I'm getting the following RSpec failed test output when I run spec spec/controllers/macroposts_controller_spec.rb: http://pastie.org/2184821 Here's the controller code for the "create" action for which the tests are failing: http://pastie.org/2184834 And here's the relevant section of the macroposts_controller_spec.rb file: http://pastie.org/2184846 Finally, here are the factory definitions referenced in the _spec.rb file given above: http://pastie.org/2184863 Focusing on failures #1 and #2, it's clear enough from the error messages that in the "POST create success" path through the specs, the @macropost object isn't receiving :save. The site has no problem saving macroposts, but when I run the tests, the "if @macropost.save" line in the controller seems to never get executed, or the save process is failing silently, perhaps due to validation problems. When I try to walk through the process in the console (in the "test" environment) I'm able to produce a valid @macropost object that saves. Yet for some reason, my test specs cannot. Any suggestions? Obvious things I've missed? Additional info I can provide? Thanks! Dean Richardson -------------- next part -------------- An HTML attachment was scrubbed... URL: From dchelimsky at gmail.com Sun Jul 10 07:00:11 2011 From: dchelimsky at gmail.com (David Chelimsky) Date: Sun, 10 Jul 2011 07:00:11 -0400 Subject: [rspec-users] Trouble with RSpec mock "@macropost.should_receive(:save).and_return(true)" -- produces error expected :save with (any args) once, but received it 0 times In-Reply-To: <2826788.299.1310161025722.JavaMail.geo-discussion-forums@yqcj24> References: <2826788.299.1310161025722.JavaMail.geo-discussion-forums@yqcj24> Message-ID: <228149F5-3BB0-4CB3-9C51-FE23271EF6E2@gmail.com> On Jul 8, 2011, at 5:37 PM, Dean wrote: > I'm getting the following RSpec failed test output when I run spec spec/controllers/macroposts_controller_spec.rb: > > http://pastie.org/2184821 > > Here's the controller code for the "create" action for which the tests are failing: > > http://pastie.org/2184834 > > And here's the relevant section of the macroposts_controller_spec.rb file: > > http://pastie.org/2184846 > > Finally, here are the factory definitions referenced in the _spec.rb file given above: > > http://pastie.org/2184863 > > Focusing on failures #1 and #2, it's clear enough from the error messages that in the "POST create success" path through the specs, the @macropost object isn't receiving :save. The site has no problem saving macroposts, but when I run the tests, the "if @macropost.save" line in the controller seems to never get executed, or the save process is failing silently, perhaps due to validation problems. > > When I try to walk through the process in the console (in the "test" environment) I'm able to produce a valid @macropost object that saves. Yet for some reason, my test specs cannot. > > Any suggestions? Obvious things I've missed? Additional info I can provide? The Project returned by "@project = Project.find(params[:project_id])" in the controller action is not the same object as the one defined in the spec. This is going to change in rails-3.1 (if you configure it to use the new identiy_map feature), but that is the case for all previous versions. To get the examples to work as written, you need to add: Project.stub(:find).and_return(@project) That should get these examples to pass, but there are a number of problems with the overall approach. One reason to use stubs is to isolate yourself from the real models. That way changes to validation rules, etc, don't cause your controller specs to fail, and your specs run significantly faster as well. Since these examples use real models, you're not getting the isolation benefit. Additionally, stubbing nested resources like this gets pretty complicated and difficult to maintain. Lastly, the controller action violates "Skinny Controller, Fat Model" [1], and that is complicating these specs quite a bit. With all that, I'd refactor the action to add the project and user IDs to the macropost hash, just interact with that model only, and move the mailer logic to an after_save hook on the model. The resulting controller and spec would look something like https://gist.github.com/1074456. Note that there is only one stub, and it is dead simple. The only thing keeping these examples slow now is the creation of a real User, but you could probably stub that out as well. HTH, David [1] http://weblog.jamisbuck.org/2006/10/18/skinny-controller-fat-model > Thanks! > > Dean Richardson -------------- next part -------------- An HTML attachment was scrubbed... URL: From jatinkumar.nitk at gmail.com Sun Jul 10 08:03:31 2011 From: jatinkumar.nitk at gmail.com (Jatin kumar) Date: Sun, 10 Jul 2011 17:33:31 +0530 Subject: [rspec-users] Trouble with RSpec mock "@macropost.should_receive(:save).and_return(true)" -- produces error expected :save with (any args) once, but received it 0 times In-Reply-To: <228149F5-3BB0-4CB3-9C51-FE23271EF6E2@gmail.com> References: <2826788.299.1310161025722.JavaMail.geo-discussion-forums@yqcj24> <228149F5-3BB0-4CB3-9C51-FE23271EF6E2@gmail.com> Message-ID: On Sun, Jul 10, 2011 at 4:30 PM, David Chelimsky wrote: > > On Jul 8, 2011, at 5:37 PM, Dean wrote: > > I'm getting the following RSpec failed test output when I run spec > spec/controllers/macroposts_controller_spec.rb: > > http://pastie.org/2184821 > > Here's the controller code for the "create" action for which the tests are > failing: > > http://pastie.org/2184834 > > And here's the relevant section of the macroposts_controller_spec.rb file: > > http://pastie.org/2184846 > > Finally, here are the factory definitions referenced in the _spec.rb file > given above: > > http://pastie.org/2184863 > > Focusing on failures #1 and #2, it's clear enough from the error messages > that in the "POST create success" path through the specs, the @macropost > object isn't receiving :save. The site has no problem saving macroposts, but > when I run the tests, the "if @macropost.save" line in the controller seems > to never get executed, or the save process is failing silently, perhaps due > to validation problems. > > When I try to walk through the process in the console (in the "test" > environment) I'm able to produce a valid @macropost object that saves. Yet > for some reason, my test specs cannot. > > Any suggestions? Obvious things I've missed? Additional info I can provide? > > > Hi David, > The Project returned by "@project = Project.find(params[:project_id])" in > the controller action is not the same object as the one defined in the spec. > This is going to change in rails-3.1 (if you configure it to use the new > identiy_map feature), > I had the same problem as above, and I used the same solution as you said.i.e. replacing should_receive with stub. However, I never got to understand why it worked. As you said, the objects returned in the spec are different but that is going to change in rails-3.1. Can you elaborate a bit on the details of the change. May be point me to a blog post written over it, and if there's not one now, why not you write yourself one explaining the whole thing, when you find time. BTW, I am a great fan of yours and your blog has helped a lot, learning the basics of TDD by reading your articles and answers here & the rspec forum. but that is the case for all previous versions. > > To get the examples to work as written, you need to add: > > Project.stub(:find).and_return(@project) > > That should get these examples to pass, but there are a number of problems > with the overall approach. > > One reason to use stubs is to isolate yourself from the real models. That > way changes to validation rules, etc, don't cause your controller specs to > fail, and your specs run significantly faster as well. Since these examples > use real models, you're not getting the isolation benefit. > > Additionally, stubbing nested resources like this gets pretty complicated > and difficult to maintain. > > Lastly, the controller action violates "Skinny Controller, Fat Model" [1], > and that is complicating these specs quite a bit. > > With all that, I'd refactor the action to add the project and user IDs to > the macropost hash, just interact with that model only, and move the mailer > logic to an after_save hook on the model. The resulting controller and spec > would look something like https://gist.github.com/1074456. Note that there > is only one stub, and it is dead simple. > > The only thing keeping these examples slow now is the creation of a real > User, but you could probably stub that out as well. > > HTH, > David > > [1] http://weblog.jamisbuck.org/2006/10/18/skinny-controller-fat-model > > > > Thanks! > > Dean Richardson > > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > -------------- next part -------------- An HTML attachment was scrubbed... URL: From dchelimsky at gmail.com Sun Jul 10 08:43:27 2011 From: dchelimsky at gmail.com (David Chelimsky) Date: Sun, 10 Jul 2011 08:43:27 -0400 Subject: [rspec-users] Trouble with RSpec mock "@macropost.should_receive(:save).and_return(true)" -- produces error expected :save with (any args) once, but received it 0 times In-Reply-To: References: <2826788.299.1310161025722.JavaMail.geo-discussion-forums@yqcj24> <228149F5-3BB0-4CB3-9C51-FE23271EF6E2@gmail.com> Message-ID: <06221702-83FA-4026-9DFA-DD15619799C3@gmail.com> On Jul 10, 2011, at 8:03 AM, Jatin kumar wrote: > On Sun, Jul 10, 2011 at 4:30 PM, David Chelimsky wrote: > > On Jul 8, 2011, at 5:37 PM, Dean wrote: > >> I'm getting the following RSpec failed test output when I run spec spec/controllers/macroposts_controller_spec.rb: >> >> http://pastie.org/2184821 >> >> Here's the controller code for the "create" action for which the tests are failing: >> >> http://pastie.org/2184834 >> >> And here's the relevant section of the macroposts_controller_spec.rb file: >> >> http://pastie.org/2184846 >> >> Finally, here are the factory definitions referenced in the _spec.rb file given above: >> >> http://pastie.org/2184863 >> >> Focusing on failures #1 and #2, it's clear enough from the error messages that in the "POST create success" path through the specs, the @macropost object isn't receiving :save. The site has no problem saving macroposts, but when I run the tests, the "if @macropost.save" line in the controller seems to never get executed, or the save process is failing silently, perhaps due to validation problems. >> >> When I try to walk through the process in the console (in the "test" environment) I'm able to produce a valid @macropost object that saves. Yet for some reason, my test specs cannot. >> >> Any suggestions? Obvious things I've missed? Additional info I can provide? > > Hi David, > > The Project returned by "@project = Project.find(params[:project_id])" in the controller action is not the same object as the one defined in the spec. This is going to change in rails-3.1 (if you configure it to use the new identiy_map feature), > > I had the same problem as above, and I used the same solution as you said.i.e. replacing should_receive with stub. However, I never got to understand why it worked. > > As you said, the objects returned in the spec are different but that is going to change in rails-3.1. > Can you elaborate a bit on the details of the change. > > May be point me to a blog post written over it, and if there's not one now, why not you write yourself one explaining the whole thing, when you find time. Google "identity map in rails 3.1" There are a lot of posts about it. > BTW, I am a great fan of yours and your blog has helped a lot, learning the basics of TDD by reading your articles and answers here & the rspec forum. > > but that is the case for all previous versions. > > To get the examples to work as written, you need to add: > > Project.stub(:find).and_return(@project) > > That should get these examples to pass, but there are a number of problems with the overall approach. > > One reason to use stubs is to isolate yourself from the real models. That way changes to validation rules, etc, don't cause your controller specs to fail, and your specs run significantly faster as well. Since these examples use real models, you're not getting the isolation benefit. > > Additionally, stubbing nested resources like this gets pretty complicated and difficult to maintain. > > Lastly, the controller action violates "Skinny Controller, Fat Model" [1], and that is complicating these specs quite a bit. > > With all that, I'd refactor the action to add the project and user IDs to the macropost hash, just interact with that model only, and move the mailer logic to an after_save hook on the model. The resulting controller and spec would look something like https://gist.github.com/1074456. Note that there is only one stub, and it is dead simple. > > The only thing keeping these examples slow now is the creation of a real User, but you could probably stub that out as well. > > HTH, > David > > [1] http://weblog.jamisbuck.org/2006/10/18/skinny-controller-fat-model > > > >> Thanks! >> >> Dean Richardson -------------- next part -------------- An HTML attachment was scrubbed... URL: From jko170 at gmail.com Sun Jul 10 22:00:32 2011 From: jko170 at gmail.com (Justin Ko) Date: Sun, 10 Jul 2011 22:00:32 -0400 Subject: [rspec-users] Rspec or Test:Unit In-Reply-To: <130dda5b-aad5-4127-9396-509c9c1b9baf@n5g2000yqh.googlegroups.com> References: <130dda5b-aad5-4127-9396-509c9c1b9baf@n5g2000yqh.googlegroups.com> Message-ID: On Fri, Jul 8, 2011 at 2:59 AM, Chandu80 wrote: > Hello All, > I want to know which out of Rspec and Test:Unit is the most used in > the industry for unit/integration testing and why? > http://22ideastreet.com/blog/2011/06/02/the-state-of-ruby-and-testing/ As for why....try them both and decide for yourself :) > > > Regards > Chandrika > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users -------------- next part -------------- An HTML attachment was scrubbed... URL: From chandu.shenoy at gmail.com Tue Jul 12 05:36:29 2011 From: chandu.shenoy at gmail.com (Chandu80) Date: Tue, 12 Jul 2011 02:36:29 -0700 (PDT) Subject: [rspec-users] How to write render expectation in Controller Spec? Message-ID: <2f49f6d6-796b-49c6-b46d-00622bc02b67@y13g2000prb.googlegroups.com> Hello All, I am a rspec beginner and I am trying to find a way out to write render expectation in controller spec.I used the following ways render '/channels/createchannel.html.erb' view.should_receive(:render).with(:action => "createchannel") ChannelsController.expect_render(:action => "createchannel") ChannelsController.render_template(:action => "createchannel") controller.expect_render(:action => "createchannel") controller.render_template(:action => "createchannel") All fail giving a NoMethodError ********************************************************************************** My spec is as follows require File.dirname(__FILE__) + '/../spec_helper' describe ChannelsController do it "should re-direct to create channel" do Channel.stub!(:checkChannel?).and_return(1) ChannelsController.expect_render(:action => "createchannel") #view.should_receive(:render).with(:action => "createchannel") #render '/channels/createchannel.html.erb' end end ********************************************************************************** Can anyone please let me know how this can be fixed.? Thanks & Regards Chandrika From chandu.shenoy at gmail.com Tue Jul 12 05:37:04 2011 From: chandu.shenoy at gmail.com (Chandu80) Date: Tue, 12 Jul 2011 02:37:04 -0700 (PDT) Subject: [rspec-users] Rspec or Test:Unit In-Reply-To: References: <130dda5b-aad5-4127-9396-509c9c1b9baf@n5g2000yqh.googlegroups.com> Message-ID: Thank you for the info :) Regards Chandrika On Jul 11, 7:00?am, Justin Ko wrote: > On Fri, Jul 8, 2011 at 2:59 AM, Chandu80 wrote: > > Hello All, > > I want to know which out of Rspec and Test:Unit is the most used in > > the industry for unit/integration testing and why? > > http://22ideastreet.com/blog/2011/06/02/the-state-of-ruby-and-testing/ > > As for why....try them both and decide for yourself :) > > > > > Regards > > Chandrika > > _______________________________________________ > > rspec-users mailing list > > rspec-us... at rubyforge.org > >http://rubyforge.org/mailman/listinfo/rspec-users > > > > _______________________________________________ > rspec-users mailing list > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users From dchelimsky at gmail.com Tue Jul 12 06:59:26 2011 From: dchelimsky at gmail.com (David Chelimsky) Date: Tue, 12 Jul 2011 05:59:26 -0500 Subject: [rspec-users] How to write render expectation in Controller Spec? In-Reply-To: <2f49f6d6-796b-49c6-b46d-00622bc02b67@y13g2000prb.googlegroups.com> References: <2f49f6d6-796b-49c6-b46d-00622bc02b67@y13g2000prb.googlegroups.com> Message-ID: <2BF77628-EA28-4053-A42A-73EF4A6AEC93@gmail.com> On Jul 12, 2011, at 4:36 AM, Chandu80 wrote: > Hello All, > I am a rspec beginner Welcome! > and I am trying to find a way out to write > render expectation in controller spec. I used the following ways > render '/channels/createchannel.html.erb' > view.should_receive(:render).with(:action => "createchannel") > ChannelsController.expect_render(:action => "createchannel") > ChannelsController.render_template(:action => "createchannel") > controller.expect_render(:action => "createchannel") > controller.render_template(:action => "createchannel") > > All fail giving a NoMethodError > > ********************************************************************************** > My spec is as follows > > require File.dirname(__FILE__) + '/../spec_helper' > > describe ChannelsController do > it "should re-direct to create channel" do > Channel.stub!(:checkChannel?).and_return(1) > ChannelsController.expect_render(:action => "createchannel") > > #view.should_receive(:render).with(:action => "createchannel") > #render '/channels/createchannel.html.erb' > end > > > end > ********************************************************************************** > > > Can anyone please let me know how this can be fixed.? Please always include the versions of Rails and RSpec that you are using when asking for help. Things are different in different versions. I'm going to assume you're using rails-3 and rspec-rails-2. The name of the example begins with "should re-direct ...". The rails testing framework lets you assert that a specific template is rendered or that there is a redirect, but not both in the same action. You also have to invoke an action on the controller in each example for anything to happen. e.g. describe "POST create" do describe "with valid params" do it "redirects to the created channel" do post :create, :channel => valid_attributes response.should redirect_to(Channel.last) end end describe "with invalid params" do it "re-renders the 'new' template" do Channel.any_instance.stub(:save).and_return(false) post :create, :channel => {} response.should render_template("new") end end end Take a look at the generated controller spec when you run 'rails generate scaffold ...': https://gist.github.com/942203. If that doesn't make sense, feel free to ask questions. HTH, David > Thanks & Regards > Chandrika From genlighten at gmail.com Tue Jul 12 14:29:42 2011 From: genlighten at gmail.com (Dean) Date: Tue, 12 Jul 2011 11:29:42 -0700 (PDT) Subject: [rspec-users] Trouble with RSpec mock "@macropost.should_receive(:save).and_return(true)" -- produces error expected :save with (any args) once, but received it 0 times In-Reply-To: <228149F5-3BB0-4CB3-9C51-FE23271EF6E2@gmail.com> References: <2826788.299.1310161025722.JavaMail.geo-discussion-forums@yqcj24> <228149F5-3BB0-4CB3-9C51-FE23271EF6E2@gmail.com> Message-ID: <2708f28f-bd92-40e4-9927-dcb5e0af65f5@a31g2000vbt.googlegroups.com> David: Wow, that's just about the perfect example of a coding help forum reply: Specifically identifies what's going wrong. Suggests a simple solution. Identifies the deeper underlying problem (code that's too bloated, tightly-coupled, or whatever, and thus hard to test) Gently offers a detailed approach to address the underlying problem All without a hint of condescension. Thank you very much! This will help improve my code in many ways beyond the present problem. --Dean On Jul 10, 6:00?am, David Chelimsky wrote: > On Jul 8, 2011, at 5:37 PM, Dean wrote: > > > > > > > I'm getting the following RSpec failed test output when I run spec spec/controllers/macroposts_controller_spec.rb: > > >http://pastie.org/2184821 > > > Here's the controller code for the "create" action for which the tests are failing: > > >http://pastie.org/2184834 > > > And here's the relevant section of the macroposts_controller_spec.rb file: > > >http://pastie.org/2184846 > > > Finally, here are the factory definitions referenced in the _spec.rb file given above: > > >http://pastie.org/2184863 > > > Focusing on failures #1 and #2, it's clear enough from the error messages that in the "POST create success" path through the specs, the @macropost object isn't receiving :save. The site has no problem saving macroposts, but when I run the tests, the "if @macropost.save" line in the controller seems to never get executed, or the save process is failing silently, perhaps due to validation problems. > > > When I try to walk through the process in the console (in the "test" environment) I'm able to produce a valid @macropost object that saves. Yet for some reason, my test specs cannot. > > > Any suggestions? Obvious things I've missed? Additional info I can provide? > > The Project returned by "@project = Project.find(params[:project_id])" in the controller action is not the same object as the one defined in the spec. This is going to change in rails-3.1 (if you configure it to use the new identiy_map feature), but that is the case for all previous versions. > > To get the examples to work as written, you need to add: > > ? Project.stub(:find).and_return(@project) > > That should get these examples to pass, but there are a number of problems with the overall approach. > > One reason to use stubs is to isolate yourself from the real models. That way changes to validation rules, etc, don't cause your controller specs to fail, and your specs run significantly faster as well. Since these examples use real models, you're not getting the isolation benefit. > > Additionally, stubbing nested resources like this gets pretty complicated and difficult to maintain. > > Lastly, the controller action violates "Skinny Controller, Fat Model" [1], and that is complicating these specs quite a bit. > > With all that, I'd refactor the action to add the project and user IDs to the macropost hash, just interact with that model only, and move the mailer logic to an after_save hook on the model. The resulting controller and spec would look something likehttps://gist.github.com/1074456. Note that there is only one stub, and it is dead simple. > > The only thing keeping these examples slow now is the creation of a real User, but you could probably stub that out as well. > > HTH, > David > > [1]http://weblog.jamisbuck.org/2006/10/18/skinny-controller-fat-model > > > > > Thanks! > > > Dean Richardson > > > > _______________________________________________ > rspec-users mailing list > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users- Hide quoted text - > > - Show quoted text -- Hide quoted text - > > - Show quoted text - From derekprior at gmail.com Tue Jul 12 15:03:30 2011 From: derekprior at gmail.com (Derek Prior) Date: Tue, 12 Jul 2011 15:03:30 -0400 Subject: [rspec-users] Specing scopes that use includes() Message-ID: I'm a newcomer to the list. Appologies if this has been discussed before, but all I could find with regards to specing scopes relates to those that filter records or apply sorting. What's the best way to spec a scope that uses the includes method? For instance: class Location < ActiveRecord.Base belongs_to: holiday_schedule scope: with_holiday_schedule, includes(:holiday_schedule) end describe Location do describe Location, "::with_holiday_schedule" it "includes holiday schedule information to avoid N+1 queries" do pending "a good way to check this..." end end end I've seen some references to hooking into ActiveSupport::Notifications for ActiveRecord SQL and then counting the SQL statements, though the code I've seen to do this is far from pretty. Does anyone have a better idea? -------------- next part -------------- An HTML attachment was scrubbed... URL: From itsterry at gmail.com Tue Jul 12 19:24:38 2011 From: itsterry at gmail.com (itsterry) Date: Tue, 12 Jul 2011 16:24:38 -0700 (PDT) Subject: [rspec-users] Rspec2: checking a view calls a helper In-Reply-To: <83f6c4cd-dd83-49f4-922c-237a6a668040@gc3g2000vbb.googlegroups.com> References: <83f6c4cd-dd83-49f4-922c-237a6a668040@gc3g2000vbb.googlegroups.com> Message-ID: I worked this out In the view spec you need to do something like this: describe "users/index.html.haml" do ? before do # this is the bit you need view.stub!(:abc).and_return(whatever_your_helper_returns) # end of the bit you need ? ? render ? end ? it "stuff" do ? ? rendered.should have_selector('whatever') ? end end On Jul 8, 1:12?am, itsterry wrote: > I have a view which calls a helper. Lets' call it "abc" > > So in my view, I write > =abc #(I'm using haml) > > When I run the view in my webserver, all works fine > > But when I run my view spec, it tells me > ? undefined local variable or method 'abc' for #<#Class:0xnumbers> > > I've googled this, but can't find the right way to tell the spec to > access the helper. Any clues, anyone? > > If it helps, here's (an edited version of) the view spec code: > > describe "users/index.html.haml" do > ? before do > ? ? render > ? end > ? it "stuff" do > ? ? rendered.should have_selector('whatever') > ? end > > end > > TIA for any help > > T > > _______________________________________________ > rspec-users mailing list > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users From chandu.shenoy at gmail.com Wed Jul 13 02:54:14 2011 From: chandu.shenoy at gmail.com (Chandu80) Date: Tue, 12 Jul 2011 23:54:14 -0700 (PDT) Subject: [rspec-users] How to write render expectation in Controller Spec? In-Reply-To: <2BF77628-EA28-4053-A42A-73EF4A6AEC93@gmail.com> References: <2f49f6d6-796b-49c6-b46d-00622bc02b67@y13g2000prb.googlegroups.com> <2BF77628-EA28-4053-A42A-73EF4A6AEC93@gmail.com> Message-ID: <33d77ec8-024a-4af8-bbcc-376e280cca8a@m6g2000prh.googlegroups.com> Thanks David...I am using jRuby on Rails1.5.0 and following are the details of gems:- rails-2.3.5 rspec-1.3.0 rspec-rails-1.3.2 My script needs to test the create channel method based on a condition given by checkChannel? which I have stubbed as true.If true it should render the action createchannel. As mentioned above I used response.should render_template("createchannel").It gave me the following error NoMethodError in 'ChannelsController should re-direct to create channel' undefined method `response' for # When I removed all statements and kept only post :createchannel in the if block,it gave me NoMethodError in 'ChannelsController should render the createchannelpage' undefined method `post' for # Is there any gem that needs to be installed that would make this work and if yes is it compatible with jRuby on Rails? Regards Chandrika On Jul 12, 3:59?pm, David Chelimsky wrote: > On Jul 12, 2011, at 4:36 AM, Chandu80 wrote: > > > Hello All, > > I am a rspec beginner > > Welcome! > > > > > and I am trying to find a way out to write > > render expectation in controller spec. I used the following ways > > render '/channels/createchannel.html.erb' > > view.should_receive(:render).with(:action => "createchannel") > > ChannelsController.expect_render(:action => "createchannel") > > ChannelsController.render_template(:action => "createchannel") > > controller.expect_render(:action => "createchannel") > > controller.render_template(:action => "createchannel") > > > All fail giving a NoMethodError > > > ********************************************************************************** > > My spec is as follows > > > require File.dirname(__FILE__) + '/../spec_helper' > > > describe ChannelsController do > > ?it "should re-direct to create channel" do > > ? ?Channel.stub!(:checkChannel?).and_return(1) > > ? ?ChannelsController.expect_render(:action => "createchannel") > > > ? ?#view.should_receive(:render).with(:action => "createchannel") > > ? ?#render '/channels/createchannel.html.erb' > > ?end > > > end > > ********************************************************************************** > > > Can anyone please let me know how this can be fixed.? > > Please always include the versions of Rails and RSpec that you are using when asking for help. Things are different in different versions. I'm going to assume you're using rails-3 and rspec-rails-2. > > The name of the example begins with "should re-direct ...". The rails testing framework lets you assert that a specific template is rendered or that there is a redirect, but not both in the same action. > > You also have to invoke an action on the controller in each example for anything to happen. e.g. > > ? describe "POST create" do > ? ? describe "with valid params" do > ? ? ? it "redirects to the created channel" do > ? ? ? ? post :create, :channel => valid_attributes > ? ? ? ? response.should redirect_to(Channel.last) > ? ? ? end > ? ? end > > ? ? describe "with invalid params" do > ? ? ? it "re-renders the 'new' template" do > ? ? ? ? Channel.any_instance.stub(:save).and_return(false) > ? ? ? ? post :create, :channel => {} > ? ? ? ? response.should render_template("new") > ? ? ? end > ? ? end > ? end > > Take a look at the generated controller spec when you run 'rails generate scaffold ...':https://gist.github.com/942203. If that doesn't make sense, feel free to ask questions. > > HTH, > David > > > Thanks & Regards > > Chandrika > > _______________________________________________ > rspec-users mailing list > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users From me at jbrains.ca Wed Jul 13 06:54:55 2011 From: me at jbrains.ca (J. B. Rainsberger) Date: Wed, 13 Jul 2011 07:54:55 -0300 Subject: [rspec-users] Specing scopes that use includes() In-Reply-To: References: Message-ID: On Tue, Jul 12, 2011 at 16:03, Derek Prior wrote: > I'm a newcomer to the list. Appologies if this has been discussed before, > but all I could find with regards to specing scopes relates to those that > filter records or apply sorting. What's the best way to spec a scope that > uses the includes method? For instance: > class Location < ActiveRecord.Base > ??belongs_to: holiday_schedule > ??scope: with_holiday_schedule, includes(:holiday_schedule) > end > describe Location do > ??describe Location, "::with_holiday_schedule" > ?? ?it "includes holiday schedule information to avoid N+1 queries" do > ?? ? ?pending "a good way to check this..." > ?? ?end > ??end > end > I've seen some references to hooking into ActiveSupport::Notifications for > ActiveRecord SQL and then counting the SQL statements, though the code I've > seen to do this is far from pretty. Does anyone have a better idea? >From what I understand, this is purely a performance enhancement, so I'd expect only to check it with performance tests. In this case, I would think that a timed test would do the job. It won't be fast, but it'll work, and you won't be tempted to use it as a behavior test, since this seems like an issue of implementation and not behavior. -- J. B. (Joe) Rainsberger :: http://www.jbrains.ca :: http://blog.thecodewhisperer.com Author, JUnit Recipes Free Your Mind to Do Great Work :: http://www.freeyourmind-dogreatwork.com From cutalion at gmail.com Wed Jul 13 11:06:33 2011 From: cutalion at gmail.com (Alexander Glushkov) Date: Wed, 13 Jul 2011 18:06:33 +0300 Subject: [rspec-users] rails: action helper for controller specs Message-ID: Hey guys, just wrote a helper for controller specs in the rails. Link to the gist - https://gist.github.com/1080421 - to view with syntax highlight. # Helper +action+ allows you to write something like # # describe "GET index" do # action { get :index } # # context 'if user signed in' do # before { sign_in user } # it { should respond_with :success } # end # # context 'if user logged out' do # it { should redirect_to sign_in_path } # end # end # # instead of # # describe "GET index" do # context 'if user signed in' do # before { sign_in user } # before { get :index } # it { should respond_with :success } # end # # context 'if user logged out' do # before { get :index } # it { should redirect_to sign_in_path } # end # end def action(&block) before { self.class.before(&block) unless action_added?(&block) } end def action_added?(&block) self.class.hooks[:before][:each].map(&:to_proc).include? block end What do you think? - Alex -------------- next part -------------- An HTML attachment was scrubbed... URL: From jko170 at gmail.com Wed Jul 13 11:30:21 2011 From: jko170 at gmail.com (Justin Ko) Date: Wed, 13 Jul 2011 11:30:21 -0400 Subject: [rspec-users] rails: action helper for controller specs In-Reply-To: References: Message-ID: On Wed, Jul 13, 2011 at 11:06 AM, Alexander Glushkov wrote: > Hey guys, > > just wrote a helper for controller specs in the rails. Link to the gist - > https://gist.github.com/1080421 - to view with syntax highlight. > > # Helper +action+ allows you to write something like > # > # describe "GET index" do > # action { get :index } > # > # context 'if user signed in' do > # before { sign_in user } > # it { should respond_with :success } > # end > # > # context 'if user logged out' do > # it { should redirect_to sign_in_path } > # end > # end > # > # instead of > # > # describe "GET index" do > # context 'if user signed in' do > # before { sign_in user } > # before { get :index } > # it { should respond_with :success } > # end > # > # context 'if user logged out' do > # before { get :index } > # it { should redirect_to sign_in_path } > # end > # end > > > def action(&block) > before { self.class.before(&block) unless action_added?(&block) } > > > > end > > def action_added?(&block) > self.class.hooks[:before][:each].map(&:to_proc).include? block > > > > end > > What do you think? > > > - Alex > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users So this is just an alias to `before`? -------------- next part -------------- An HTML attachment was scrubbed... URL: From cutalion at gmail.com Wed Jul 13 13:22:31 2011 From: cutalion at gmail.com (Alexander Glushkov) Date: Wed, 13 Jul 2011 10:22:31 -0700 (PDT) Subject: [rspec-users] rails: action helper for controller specs In-Reply-To: Message-ID: <29138533.1620.1310577751423.JavaMail.geo-discussion-forums@yqad28> Not exactly. It adds a before { 'your action call' } to the end of hooks collection, so that it will called at the very end, right before the spec call. Compare two examples I provided in the comments. -------------- next part -------------- An HTML attachment was scrubbed... URL: From lists at ruby-forum.com Thu Jul 14 07:34:14 2011 From: lists at ruby-forum.com (Chandrika Shenoy) Date: Thu, 14 Jul 2011 13:34:14 +0200 Subject: [rspec-users] Render expectation in Controller spec Message-ID: <3aafbd513dca9f92c0365e02effa257e@ruby-forum.com> Hello All, I am a rspec beginner and I am trying to find a way out to write render expectation in controller spec.All methods that I used seem to give me a No Method error. My controller code is as follows http://pastie.org/2211944 and my controller test is as follows http://pastie.org/2211951 I also used render '/channels/createchannel.html.erb' ChannelsController.expect_render(:action => "createchannel") ChannelsController.render_template(:action => "createchannel") controller.expect_render(:action => "createchannel") controller.render_template(:action => "createchannel") I am using jRuby on Rails1.5.0 and following are the details of gems:- rails-2.3.5 rspec-1.3.0 rspec-rails-1.3.2 It would be great if someone could help me out with this Thanks & Regards Chandrika -- Posted via http://www.ruby-forum.com/. From dchelimsky at gmail.com Thu Jul 14 08:30:11 2011 From: dchelimsky at gmail.com (David Chelimsky) Date: Thu, 14 Jul 2011 07:30:11 -0500 Subject: [rspec-users] Render expectation in Controller spec In-Reply-To: <3aafbd513dca9f92c0365e02effa257e@ruby-forum.com> References: <3aafbd513dca9f92c0365e02effa257e@ruby-forum.com> Message-ID: <5F84A46D-945C-46D6-9647-EB48FC53F8B6@gmail.com> On Jul 14, 2011, at 6:34 AM, Chandrika Shenoy wrote: > Hello All, > > I am a rspec beginner and I am trying to find a way out to write render > expectation in controller spec.All methods that I used seem to give me a > No Method error. > > My controller code is as follows > http://pastie.org/2211944 > > and my controller test is as follows > http://pastie.org/2211951 I don't understand what the example is supposed to specify. I see a createchannelpage action in the controller, which might render createchannel, but I don't see a createchannel action. Then the spec is posting to createchannel. What is the intent here? > I also used > render '/channels/createchannel.html.erb' > ChannelsController.expect_render(:action => "createchannel") > ChannelsController.render_template(:action => "createchannel") > controller.expect_render(:action => "createchannel") > controller.render_template(:action => "createchannel") > > I am using jRuby on Rails1.5.0 and following are the > details of gems:- > rails-2.3.5 > rspec-1.3.0 > rspec-rails-1.3.2 > > It would be great if someone could help me out with this From chandu.shenoy at gmail.com Fri Jul 15 07:06:41 2011 From: chandu.shenoy at gmail.com (Chandu80) Date: Fri, 15 Jul 2011 04:06:41 -0700 (PDT) Subject: [rspec-users] Render expectation in Controller spec In-Reply-To: <5F84A46D-945C-46D6-9647-EB48FC53F8B6@gmail.com> References: <3aafbd513dca9f92c0365e02effa257e@ruby-forum.com> <5F84A46D-945C-46D6-9647-EB48FC53F8B6@gmail.com> Message-ID: Hi David, Sorry about not pasting the createchannel method earlier,here is the revised controller method. http://pastie.org/2217223 I am trying to test the createchannelpage method which is in the Channels Controller.The createchannelpage renders the createchannel action when the checkChannel? returns a true value. Regards Chandrika On Jul 14, 5:30?pm, David Chelimsky wrote: > On Jul 14, 2011, at 6:34 AM, Chandrika Shenoy wrote: > > > Hello All, > > > I am a rspec beginner and I am trying to find a way out to write render > > expectation in controller spec.All methods that I used seem to give me a > > No Method error. > > > My controller code is as follows > >http://pastie.org/2211944 > > > and my controller test is as follows > >http://pastie.org/2211951 > > I don't understand what the example is supposed to specify. I see a createchannelpage action in the controller, which might render createchannel, but I don't see a createchannel action. Then the spec is posting to createchannel. What is the intent here? > > > I also used > > render '/channels/createchannel.html.erb' > > ChannelsController.expect_render(:action => "createchannel") > > ChannelsController.render_template(:action => "createchannel") > > controller.expect_render(:action => "createchannel") > > controller.render_template(:action => "createchannel") > > > I am using jRuby on Rails1.5.0 and following are the > > details of gems:- > > rails-2.3.5 > > rspec-1.3.0 > > rspec-rails-1.3.2 > > > It would be great if someone could help me out with this > > _______________________________________________ > rspec-users mailing list > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users From dchelimsky at gmail.com Fri Jul 15 08:29:41 2011 From: dchelimsky at gmail.com (David Chelimsky) Date: Fri, 15 Jul 2011 07:29:41 -0500 Subject: [rspec-users] Render expectation in Controller spec In-Reply-To: References: <3aafbd513dca9f92c0365e02effa257e@ruby-forum.com> <5F84A46D-945C-46D6-9647-EB48FC53F8B6@gmail.com> Message-ID: <35C173FF-D747-4B23-B708-B0926E9D031C@gmail.com> I moved your post to the bottom - please post either at the bottom or inline so we can follow the conversation more easily. On Jul 15, 2011, at 6:06 AM, Chandu80 wrote: > On Jul 14, 5:30 pm, David Chelimsky wrote: >> On Jul 14, 2011, at 6:34 AM, Chandrika Shenoy wrote: >> >>> Hello All, >> >>> I am a rspec beginner and I am trying to find a way out to write render >>> expectation in controller spec.All methods that I used seem to give me a >>> No Method error. >> >>> My controller code is as follows >>> http://pastie.org/2211944 >> >>> and my controller test is as follows >>> http://pastie.org/2211951 >> >> I don't understand what the example is supposed to specify. I see a createchannelpage action in the controller, which might render createchannel, but I don't see a createchannel action. Then the spec is posting to createchannel. What is the intent here? > Hi David, > Sorry about not pasting the createchannel method earlier,here is the > revised controller method. > > http://pastie.org/2217223 > > I am trying to test the createchannelpage method which is in the > Channels Controller.The createchannelpage renders the createchannel > action when the checkChannel? returns a true value. But the spec is still invoking createchannel directly. I think you want it to invoke createchannelpage, which should then render the createchannel template, right? If so, the spec should read: post :createchannelpage response.should render_template("createchannel") Does that work? From kedarwaghmode at gmail.com Mon Jul 11 19:13:45 2011 From: kedarwaghmode at gmail.com (KWIO) Date: Mon, 11 Jul 2011 16:13:45 -0700 (PDT) Subject: [rspec-users] [Cucumber] ANN: Cucumber with pure Java In-Reply-To: <3133DE2A-6697-4679-8BED-08B30C6B25F5@benmabey.com> References: <8d961d900903250808p46a2ca82ke23e8c8975dd6ff3@mail.gmail.com> <3133DE2A-6697-4679-8BED-08B30C6B25F5@benmabey.com> Message-ID: <32042148.post@talk.nabble.com> Hi, None of these example links are working,can you please check? Thanks for your help, Kedar Ben Mabey-2 wrote: > > > On Mar 25, 2009, at 9:08 AM, aslak hellesoy wrote: > >> Big news for all Java programmers out there. >> Now you can use Cucumber with pure Java! >> >> That's right, you don't have to write a single line of Ruby! (1) >> All of your step definitions can be written as annotated methods in >> POJOs (Plain Old Java Objects). >> >> To get a taste of what this looks like, check out the simple example >> in the cucumber_java project on GitHub: >> >> * README for example: >> http://github.com/aslakhellesoy/cucumber_java/tree/271160300da5bc9275dd67624f711c5ea6913187/examples/simple >> * Some sample step defs: >> http://github.com/aslakhellesoy/cucumber_java/blob/271160300da5bc9275dd67624f711c5ea6913187/examples/simple/src/main/java/simple/StuffSteps.java >> * The main page: >> http://github.com/aslakhellesoy/cucumber_java/tree/master >> >> By this I hope Cucumber will reach a bigger crowd. Much bigger crowd >> actually :-) - this will work for Scala, Clojure, Jython and all the >> other cool JVM languages too. >> (I'll be doing something similar for .NET, which will bring Cucumber >> goodness to C#, F# and whatever languages run on .NET. But IronRuby >> must catch up on speed first). >> >> I had the pleasure of going back to visit PicoContainer - a >> pioneering lightweight dependency injection container I developed >> with Paul Hammant back in 2003. It's still lightweight and a joy to >> use (relatively speaking - Java in itself isn't joyful). Cucumber- >> java uses PicoContainer to instantiate the POJOs that define the >> annotated step definition methods, so if you're so inclined, these >> classes can depend on each other via constructor DI, and >> PicoContainer just figures out how to instantiate the objects). >> >> So if you're working on a Java project, have been looking at >> Cucumber but stayed away because of all the weird Ruby, this is your >> chance. Here is a cuke for Duke! >> >> (::) Aslak (::) >> >> (1) You still need a tiny bit of Ruby to register step definitions: >> http://github.com/aslakhellesoy/cucumber_java/blob/271160300da5bc9275dd67624f711c5ea6913187/examples/simple/features/support/env.rb >> (This will hopefully go away in the future, with some better Ant and >> Maven support). > > > Very cool stuff. It would be an interesting and informative exercise > to take a JBehave tutorial and use Cucumber to drive out the same > feature and then have a side-by-side comparison of the required step > definitions. I found this JBehave tutorial which would make a good > candidate for such a comparison: > http://www.ryangreenhall.com/articles/bdd-by-example.html > > If someone doesn't get around to this in the next couple of weeks I > may take a stab at it and make a blog post of it. > > -Ben > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > -- View this message in context: http://old.nabble.com/-Cucumber--ANN%3A-Cucumber-with-pure-Java-tp22704377p32042148.html Sent from the rspec-users mailing list archive at Nabble.com. From dzhang21 at gmail.com Mon Jul 11 21:38:42 2011 From: dzhang21 at gmail.com (David Zhang) Date: Mon, 11 Jul 2011 18:38:42 -0700 (PDT) Subject: [rspec-users] How do you deal with @meal.restaurant_id = ... in such a controller test? Message-ID: <27999263.4734.1310434722405.JavaMail.geo-discussion-forums@yqkc21> I'm wondering how you tell RSpec to ignore null objects entirely. For instance, I have this restaurant_id call below: def create @meal = Meal.new(params[:meal]) @meal.restaurant_id = current_user.restaurant_id # automatically decide the restaurant-meal relationship if @meal.save flash[:notice] = "The meal was saved successfully." redirect_to :action => :index else render :action => :new end end ...and I'm not sure how to deal with it. My spec test looks like this right now: describe "POST create" do let(:meal) { mock_model(Meal).as_null_object } before(:each) do Meal.stub(:new).and_return(meal) # meal.stub(:restaurant_id) Not sure what to do here!!!!!!!! end it "creates a new meal" do Meal.should_receive(:new).with("name" => "Pizza").and_return(meal) post :create, :meal => { "name" => "Pizza" } end context "when the meal saves successfully" do before(:each) do # just for balance; as_null_object causes a truthy value to be returned anyway meal.stub(:save).and_return(true) end it "sets a flash message" do post :create flash[:notice].should eq("The meal was saved successfully.") end it "redirects to the meals index" do post :create response.should redirect_to(:action => :index) end end context "when the meal fails to save" do before(:each) do meal.stub(:save).and_return(false) end it "assigns @meal" do post :create assigns[:meal].should eq(meal) end it "renders the new template" do post :create response.should render_template("new") end end end If anyone knows the best way to deal with this, please let me know. I really want to adeptly use RSpec in the future. -------------- next part -------------- An HTML attachment was scrubbed... URL: From aslak.hellesoy at gmail.com Sat Jul 16 08:45:09 2011 From: aslak.hellesoy at gmail.com (=?UTF-8?Q?Aslak_Helles=C3=B8y?=) Date: Sat, 16 Jul 2011 13:45:09 +0100 Subject: [rspec-users] [Cucumber] ANN: Cucumber with pure Java In-Reply-To: <32042148.post@talk.nabble.com> References: <8d961d900903250808p46a2ca82ke23e8c8975dd6ff3@mail.gmail.com> <3133DE2A-6697-4679-8BED-08B30C6B25F5@benmabey.com> <32042148.post@talk.nabble.com> Message-ID: <-5716527285388264148@unknownmsgid> On Jul 16, 2011, at 12:57, KWIO wrote: > > Hi, > > None of these example links are working,can you please check? > http://github.com/cucumber/cuke4duke > Thanks for your help, > Kedar > > > Ben Mabey-2 wrote: >> >> >> On Mar 25, 2009, at 9:08 AM, aslak hellesoy wrote: >> >>> Big news for all Java programmers out there. >>> Now you can use Cucumber with pure Java! >>> >>> That's right, you don't have to write a single line of Ruby! (1) >>> All of your step definitions can be written as annotated methods in >>> POJOs (Plain Old Java Objects). >>> >>> To get a taste of what this looks like, check out the simple example >>> in the cucumber_java project on GitHub: >>> >>> * README for example: >>> http://github.com/aslakhellesoy/cucumber_java/tree/271160300da5bc9275dd67624f711c5ea6913187/examples/simple >>> * Some sample step defs: >>> http://github.com/aslakhellesoy/cucumber_java/blob/271160300da5bc9275dd67624f711c5ea6913187/examples/simple/src/main/java/simple/StuffSteps.java >>> * The main page: >>> http://github.com/aslakhellesoy/cucumber_java/tree/master >>> >>> By this I hope Cucumber will reach a bigger crowd. Much bigger crowd >>> actually :-) - this will work for Scala, Clojure, Jython and all the >>> other cool JVM languages too. >>> (I'll be doing something similar for .NET, which will bring Cucumber >>> goodness to C#, F# and whatever languages run on .NET. But IronRuby >>> must catch up on speed first). >>> >>> I had the pleasure of going back to visit PicoContainer - a >>> pioneering lightweight dependency injection container I developed >>> with Paul Hammant back in 2003. It's still lightweight and a joy to >>> use (relatively speaking - Java in itself isn't joyful). Cucumber- >>> java uses PicoContainer to instantiate the POJOs that define the >>> annotated step definition methods, so if you're so inclined, these >>> classes can depend on each other via constructor DI, and >>> PicoContainer just figures out how to instantiate the objects). >>> >>> So if you're working on a Java project, have been looking at >>> Cucumber but stayed away because of all the weird Ruby, this is your >>> chance. Here is a cuke for Duke! >>> >>> (::) Aslak (::) >>> >>> (1) You still need a tiny bit of Ruby to register step definitions: >>> http://github.com/aslakhellesoy/cucumber_java/blob/271160300da5bc9275dd67624f711c5ea6913187/examples/simple/features/support/env.rb >>> (This will hopefully go away in the future, with some better Ant and >>> Maven support). >> >> >> Very cool stuff. It would be an interesting and informative exercise >> to take a JBehave tutorial and use Cucumber to drive out the same >> feature and then have a side-by-side comparison of the required step >> definitions. I found this JBehave tutorial which would make a good >> candidate for such a comparison: >> http://www.ryangreenhall.com/articles/bdd-by-example.html >> >> If someone doesn't get around to this in the next couple of weeks I >> may take a stab at it and make a blog post of it. >> >> -Ben >> >> >> _______________________________________________ >> rspec-users mailing list >> rspec-users at rubyforge.org >> http://rubyforge.org/mailman/listinfo/rspec-users >> > > -- > View this message in context: http://old.nabble.com/-Cucumber--ANN%3A-Cucumber-with-pure-Java-tp22704377p32042148.html > Sent from the rspec-users mailing list archive at Nabble.com. > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users From lists at ruby-forum.com Sat Jul 16 09:35:55 2011 From: lists at ruby-forum.com (kevin peter) Date: Sat, 16 Jul 2011 15:35:55 +0200 Subject: [rspec-users] How to test block parameters which is sending to method with arguments?? Message-ID: Hai I would like to know how to test block parameters send to method with arguments... Problem: Here i would like to get weather report basing on city_name. here my method name is request and i am sending :get_cities_weather as argument to that method. @cities_weather = request(:get_cities_weather) do soap.body = { weather_request =>{:city_name => "Banglore" } } end Issue: Here My problem is with block and i would like to know how to test block params i.e soap.body = { weather_request =>{:city_name => "Banglore" } } Here i need to test soap.body key name i.e "weather_request" wheather i am sending exact key name or not?? Could u please help me how to test it??????????? -- Posted via http://www.ruby-forum.com/. From dchelimsky at gmail.com Sat Jul 16 13:47:38 2011 From: dchelimsky at gmail.com (David Chelimsky) Date: Sat, 16 Jul 2011 12:47:38 -0500 Subject: [rspec-users] How to test block parameters which is sending to method with arguments?? In-Reply-To: References: Message-ID: <0BF9E1D9-F5A6-4F17-9256-B557FB55204F@gmail.com> On Jul 16, 2011, at 8:35 AM, kevin peter wrote: > Hai > > I would like to know how to test block parameters send to method with > arguments... > > Problem: > > Here i would like to get weather report basing on city_name. > > here my method name is request and i am sending :get_cities_weather > as argument to that method. > > @cities_weather = request(:get_cities_weather) do > soap.body = { weather_request =>{:city_name => "Banglore" } } > end > > Issue: > > Here My problem is with block and i would like to know how to test block params i.e > > soap.body = { weather_request =>{:city_name => "Banglore" } } > > Here i need to test soap.body key name i.e "weather_request" whether i am sending exact key name or not?? > > Could u please help me how to test it??????????? Hey Kevin, It's difficult to know how to go about this without more context. Would you post the full content (https://gist.github.com or http://pastie.org please) of the file where this code lives? If not the full file, at least the method it is in and notes about any instance variables and/or methods (i.e. I can guess what 'soap' might be in this case, but I have no way of knowing for certain, and that will make a big difference in how to proceed). Thx, David From dchelimsky at gmail.com Sat Jul 16 13:49:38 2011 From: dchelimsky at gmail.com (David Chelimsky) Date: Sat, 16 Jul 2011 12:49:38 -0500 Subject: [rspec-users] How do you deal with @meal.restaurant_id = ... in such a controller test? In-Reply-To: <27999263.4734.1310434722405.JavaMail.geo-discussion-forums@yqkc21> References: <27999263.4734.1310434722405.JavaMail.geo-discussion-forums@yqkc21> Message-ID: <323DE0AD-A548-42D8-98C0-4CE6E7FA611F@gmail.com> On Jul 11, 2011, at 8:38 PM, David Zhang wrote: > I'm wondering how you tell RSpec to ignore null objects entirely. > For instance, I have this restaurant_id call below: > > def create > @meal = Meal.new(params[:meal]) > @meal.restaurant_id = current_user.restaurant_id # automatically decide the restaurant-meal relationship > if @meal.save > flash[:notice] = "The meal was saved successfully." > redirect_to :action => :index > else > render :action => :new > end > end > > ...and I'm not sure how to deal with it. My spec test looks like this right now: > > describe "POST create" do > let(:meal) { mock_model(Meal).as_null_object } > > before(:each) do > Meal.stub(:new).and_return(meal) > # meal.stub(:restaurant_id) Not sure what to do here!!!!!!!! > end > > it "creates a new meal" do > Meal.should_receive(:new).with("name" => "Pizza").and_return(meal) > post :create, :meal => { "name" => "Pizza" } > end > > context "when the meal saves successfully" do > before(:each) do # just for balance; as_null_object causes a truthy value to be returned anyway > meal.stub(:save).and_return(true) > end > > it "sets a flash message" do > post :create > flash[:notice].should eq("The meal was saved successfully.") > end > > it "redirects to the meals index" do > post :create > response.should redirect_to(:action => :index) > end > end > > context "when the meal fails to save" do > before(:each) do > meal.stub(:save).and_return(false) > end > > it "assigns @meal" do > post :create > assigns[:meal].should eq(meal) > end > > it "renders the new template" do > post :create > response.should render_template("new") > end > end > > end > > If anyone knows the best way to deal with this, please let me know. I really want to adeptly use RSpec in the future. What's the failure message you're getting? From wilsonb at gmail.com Sat Jul 16 15:58:07 2011 From: wilsonb at gmail.com (Wilson Bilkovich) Date: Sat, 16 Jul 2011 15:58:07 -0400 Subject: [rspec-users] How do you deal with @meal.restaurant_id = ... in such a controller test? In-Reply-To: <323DE0AD-A548-42D8-98C0-4CE6E7FA611F@gmail.com> References: <27999263.4734.1310434722405.JavaMail.geo-discussion-forums@yqkc21> <323DE0AD-A548-42D8-98C0-4CE6E7FA611F@gmail.com> Message-ID: Well, he's basically got a conditional somewhere: if foo.baz else end ..and he wants to use a proxy object as the return value of 'baz' instead of nil. Unfortunately, MRI just won't let you do that, so he needs to set different expectations. That's my understanding of the question, at least. --Wilson. On Sat, Jul 16, 2011 at 1:49 PM, David Chelimsky wrote: > > On Jul 11, 2011, at 8:38 PM, David Zhang wrote: > >> I'm wondering how you tell RSpec to ignore null objects entirely. >> For instance, I have this restaurant_id call below: >> >> ? def create >> ? ? @meal = Meal.new(params[:meal]) >> ? ? @meal.restaurant_id = current_user.restaurant_id # automatically decide the restaurant-meal relationship >> ? ? if @meal.save >> ? ? ? flash[:notice] = "The meal was saved successfully." >> ? ? ? redirect_to :action => :index >> ? ? else >> ? ? ? render :action => :new >> ? ? end >> ? end >> >> ...and I'm not sure how to deal with it. ?My spec test looks like this right now: >> >> ? describe "POST create" do >> ? ? let(:meal) { mock_model(Meal).as_null_object } >> >> ? ? before(:each) do >> ? ? ? Meal.stub(:new).and_return(meal) >> ? ? ? # meal.stub(:restaurant_id) Not sure what to do here!!!!!!!! >> ? ? end >> >> ? ? it "creates a new meal" do >> ? ? ? Meal.should_receive(:new).with("name" => "Pizza").and_return(meal) >> ? ? ? post :create, :meal => { "name" => "Pizza" } >> ? ? end >> >> ? ? context "when the meal saves successfully" do >> ? ? ? before(:each) do # just for balance; as_null_object causes a truthy value to be returned anyway >> ? ? ? ? meal.stub(:save).and_return(true) >> ? ? ? end >> >> ? ? ? it "sets a flash message" do >> ? ? ? ? post :create >> ? ? ? ? flash[:notice].should eq("The meal was saved successfully.") >> ? ? ? end >> >> ? ? ? it "redirects to the meals index" do >> ? ? ? ? post :create >> ? ? ? ? response.should redirect_to(:action => :index) >> ? ? ? end >> ? ? end >> >> ? ? context "when the meal fails to save" do >> ? ? ? before(:each) do >> ? ? ? ? meal.stub(:save).and_return(false) >> ? ? ? end >> >> ? ? ? it "assigns @meal" do >> ? ? ? ? post :create >> ? ? ? ? assigns[:meal].should eq(meal) >> ? ? ? end >> >> ? ? ? it "renders the new template" do >> ? ? ? ? post :create >> ? ? ? ? response.should render_template("new") >> ? ? ? end >> ? ? end >> >> ? end >> >> If anyone knows the best way to deal with this, please let me know. ?I really want to adeptly use RSpec in the future. > > What's the failure message you're getting? > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > From dchelimsky at gmail.com Sat Jul 16 16:08:20 2011 From: dchelimsky at gmail.com (David Chelimsky) Date: Sat, 16 Jul 2011 15:08:20 -0500 Subject: [rspec-users] How do you deal with @meal.restaurant_id = ... in such a controller test? In-Reply-To: References: <27999263.4734.1310434722405.JavaMail.geo-discussion-forums@yqkc21> <323DE0AD-A548-42D8-98C0-4CE6E7FA611F@gmail.com> Message-ID: <6437118B-8765-48C0-82FE-AF5B48924DA9@gmail.com> On Jul 16, 2011, at 2:58 PM, Wilson Bilkovich wrote: > On Sat, Jul 16, 2011 at 1:49 PM, David Chelimsky wrote: >> >> On Jul 11, 2011, at 8:38 PM, David Zhang wrote: >> >>> I'm wondering how you tell RSpec to ignore null objects entirely. >>> For instance, I have this restaurant_id call below: >>> >>> def create >>> @meal = Meal.new(params[:meal]) >>> @meal.restaurant_id = current_user.restaurant_id # automatically decide the restaurant-meal relationship >>> if @meal.save >>> flash[:notice] = "The meal was saved successfully." >>> redirect_to :action => :index >>> else >>> render :action => :new >>> end >>> end >>> >>> ...and I'm not sure how to deal with it. My spec test looks like this right now: >>> >>> describe "POST create" do >>> let(:meal) { mock_model(Meal).as_null_object } >>> >>> before(:each) do >>> Meal.stub(:new).and_return(meal) >>> # meal.stub(:restaurant_id) Not sure what to do here!!!!!!!! >>> end >>> >>> it "creates a new meal" do >>> Meal.should_receive(:new).with("name" => "Pizza").and_return(meal) >>> post :create, :meal => { "name" => "Pizza" } >>> end >>> >>> context "when the meal saves successfully" do >>> before(:each) do # just for balance; as_null_object causes a truthy value to be returned anyway >>> meal.stub(:save).and_return(true) >>> end >>> >>> it "sets a flash message" do >>> post :create >>> flash[:notice].should eq("The meal was saved successfully.") >>> end >>> >>> it "redirects to the meals index" do >>> post :create >>> response.should redirect_to(:action => :index) >>> end >>> end >>> >>> context "when the meal fails to save" do >>> before(:each) do >>> meal.stub(:save).and_return(false) >>> end >>> >>> it "assigns @meal" do >>> post :create >>> assigns[:meal].should eq(meal) >>> end >>> >>> it "renders the new template" do >>> post :create >>> response.should render_template("new") >>> end >>> end >>> >>> end >>> >>> If anyone knows the best way to deal with this, please let me know. I really want to adeptly use RSpec in the future. >> >> What's the failure message you're getting? >> > Well, he's basically got a conditional somewhere: > if foo.baz > else > end > ..and he wants to use a proxy object as the return value of 'baz' > instead of nil. > Unfortunately, MRI just won't let you do that, so he needs to set > different expectations. > > That's my understanding of the question, at least. > --Wilson. Depends on the failure message. If it is that current_user is nil, then the fix is to set up or stub the current user. If it's that the mock object doesn't respond to restaurant_id= (not restaurant_id, which is what the example is stubbing), then the fix is to stub restaurant_id=. Might end up being both are problems, but I don't know what else is going on elsewhere in the setup. That's why I was asking about the failure message :) -------------- next part -------------- An HTML attachment was scrubbed... URL: From lists at ruby-forum.com Mon Jul 18 02:04:12 2011 From: lists at ruby-forum.com (kevin peter) Date: Mon, 18 Jul 2011 08:04:12 +0200 Subject: [rspec-users] How to test block parameters which is sending to method with arguments?? In-Reply-To: <0BF9E1D9-F5A6-4F17-9256-B557FB55204F@gmail.com> References: <0BF9E1D9-F5A6-4F17-9256-B557FB55204F@gmail.com> Message-ID: Hi David i have given detailed explanation about my problem. with below link http://pastie.org/2230103.. Could u please help me??? Thanks in advance -- Posted via http://www.ruby-forum.com/. From matt at mattwynne.net Mon Jul 18 07:30:50 2011 From: matt at mattwynne.net (Matt Wynne) Date: Mon, 18 Jul 2011 12:30:50 +0100 Subject: [rspec-users] How to test block parameters which is sending to method with arguments?? In-Reply-To: References: <0BF9E1D9-F5A6-4F17-9256-B557FB55204F@gmail.com> Message-ID: <5E646C3A-E75A-4478-8A51-F84BCCF3EADF@mattwynne.net> On 18 Jul 2011, at 07:04, kevin peter wrote: > Hi David > i have given detailed explanation about my problem. with below link > http://pastie.org/2230103.. > Could u please help me??? > > Thanks in advance I had a quick look at this, and I have three bits of advice: 1) Hire a consultant like @JBrains to come and give you and your team some training in how to solve these kinds of problems for yourselves 2) Buy Michael Feathers' "Working Effectively With Legacy Code" and read it. 3) Buy Steve Freeman & Nat Pryce's "Growing Object Oriented Software, Guided By Tests" and read it. What you need to understand about here is how to create a "Seam" between your code and the SOAP library you're using. The reason it's painful for you to mock against this library is that is wasn't designed with that in mind. Both the Feathers book and the Freeman / Pryce book will teach you how to do that. cheers, Matt -- Freelance programmer & coach Author, http://pragprog.com/books/hwcuc/the-cucumber-book (with Aslak Helles?y) Founder, http://relishapp.com +44(0)7974430184 | http://twitter.com/mattwynne From samwgoldman at gmail.com Mon Jul 18 19:33:56 2011 From: samwgoldman at gmail.com (Sam Goldman) Date: Mon, 18 Jul 2011 19:33:56 -0400 Subject: [rspec-users] Testing complex-form-example helpers Message-ID: I am new to test-driven development and rspec, so I am writing an application[1] to practice. One of the patterns I am trying to implement is from Ryan Bates' complex-form-examples[2]. Ryan's example doesn't have any tests, so I am trying to write tests for the code. I am starting with a helper function that he wrote, which makes it easy to add fields for an association into a form. The method does a little reflection, renders a fields_for call into a string, and then returns a link_to_function which calls a custom javascript function that adds the fields to the form. My question is specifically in regard to testing this method in the helper. I have written a test which is green, but I am not sure if it is a good test. Since I am new, I would greatly benefit to some opinions and critiques of my work. Some concerns that I have: * I am doing a lot of method expectations, and doing so makes me think that I am doing something wrong. * My test covers functionality specific to the association between Evaluation and Criterion, while the code is generic. The spec that I came up with: https://github.com/samwgoldman/gradebook/blob/59b48ed4c973d135f54086ad483c09b6e34aec75/spec/helpers/application_helper_spec.rb The helper method those tests produced: https://github.com/samwgoldman/gradebook/blob/59b48ed4c973d135f54086ad483c09b6e34aec75/app/helpers/application_helper.rb I have a higher-level cucumber test as well, which I am relying on for the javascript testing: https://github.com/samwgoldman/gradebook/blob/59b48ed4c973d135f54086ad483c09b6e34aec75/features/create_evaluation.feature#L26 I learned a lot about RSpec doing this, but I am sure that with a few opinions and critiques I could learn a lot more. Thanks, Sam 1. https://github.com/samwgoldman/gradebook 2. https://github.com/ryanb/complex-form-examples From patrick at collinatorstudios.com Mon Jul 18 20:03:23 2011 From: patrick at collinatorstudios.com (Patrick J. Collins) Date: Mon, 18 Jul 2011 17:03:23 -0700 (PDT) Subject: [rspec-users] Testing complex-form-example helpers In-Reply-To: References: Message-ID: > I am new to test-driven development and rspec, so I am writing an > application[1] to practice. One of the patterns I am trying to > implement is from Ryan Bates' complex-form-examples[2]. Ryan's example > doesn't have any tests, so I am trying to write tests for the code. Don't forget that the railscast episode you are referring to is quite old and has a much better modern solution: http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html Though maybe you are aware of that and still want to work out this particular problem manually with behavior driven development? Patrick J. Collins http://collinatorstudios.com From derekprior at gmail.com Mon Jul 18 20:20:51 2011 From: derekprior at gmail.com (Derek Prior) Date: Mon, 18 Jul 2011 20:20:51 -0400 Subject: [rspec-users] Testing complex-form-example helpers In-Reply-To: References: Message-ID: > I am new to test-driven development and rspec, so I am writing an > application[1] to practice. Me too, so the advice I offer below should be taken in that light. Less advice and more, "What I think I kind of figured out." > One of the patterns I am trying to > implement is from Ryan Bates' complex-form-examples[2]. Ryan's example > doesn't have any tests, so I am trying to write tests for the code. > He's updated that with a later episode series. Search his site for "nested" and it's the first hit, I believe. I am starting with a helper function that he wrote, which makes it > easy to add fields for an association into a form. The method does a > little reflection, renders a fields_for call into a string, and then > returns a link_to_function which calls a custom javascript function > that adds the fields to the form. > > The realization I'm coming to is that a helper spec isn't necessarily the best thing to cover this type of thing. RSpec is about specifying software behavior. The behavior you want to test is, if I recall correctly from the episode, that when the user clicks a link in the page, new association fields should be added to the form. I would likely skip the helper spec and test this with a request spec or cucumber scenario using capybara & selenium. It's a fine, difficult to define, line, I think. If you're too liberal you could end up testing everything via request specs. If you're too conservative you end up writing fairly complex specs that don't provide the value they really should. As of now, I spec everything in my models and controllers, and tend to spec only very complex helper behavior. Though usually, I end up refactoring the model and/or controller to make the helper less complex instead. As I said - this is just another n00b's interpretation. Interested to hear other responses... Some concerns that I have: > * I am doing a lot of method expectations, and doing so makes me think > that I am doing something wrong. > * My test covers functionality specific to the association between > Evaluation and Criterion, while the code is generic. > > The spec that I came up with: > > https://github.com/samwgoldman/gradebook/blob/59b48ed4c973d135f54086ad483c09b6e34aec75/spec/helpers/application_helper_spec.rb > > The helper method those tests produced: > > https://github.com/samwgoldman/gradebook/blob/59b48ed4c973d135f54086ad483c09b6e34aec75/app/helpers/application_helper.rb > > I have a higher-level cucumber test as well, which I am relying on for > the javascript testing: > > https://github.com/samwgoldman/gradebook/blob/59b48ed4c973d135f54086ad483c09b6e34aec75/features/create_evaluation.feature#L26 > > I learned a lot about RSpec doing this, but I am sure that with a few > opinions and critiques I could learn a lot more. > > Thanks, > Sam > > 1. https://github.com/samwgoldman/gradebook > 2. https://github.com/ryanb/complex-form-examples > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > -------------- next part -------------- An HTML attachment was scrubbed... URL: From samwgoldman at gmail.com Mon Jul 18 21:59:52 2011 From: samwgoldman at gmail.com (Sam Goldman) Date: Mon, 18 Jul 2011 21:59:52 -0400 Subject: [rspec-users] Testing complex-form-example helpers In-Reply-To: References: Message-ID: Patrick, The example I linked to does use nested attributes. See https://github.com/ryanb/complex-form-examples/blob/master/app/models/survey.rb for the model class which uses it. Sam On Mon, Jul 18, 2011 at 8:03 PM, Patrick J. Collins wrote: >> I am new to test-driven development and rspec, so I am writing an >> application[1] to practice. One of the patterns I am trying to >> implement is from Ryan Bates' complex-form-examples[2]. Ryan's example >> doesn't have any tests, so I am trying to write tests for the code. > Don't forget that the railscast episode you are referring to is quite old and > has a much better modern solution: > http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html > > Though maybe you are aware of that and still want to work out this particular > problem manually with behavior driven development? > > Patrick J. Collins > http://collinatorstudios.com > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > From samwgoldman at gmail.com Mon Jul 18 22:19:05 2011 From: samwgoldman at gmail.com (Sam Goldman) Date: Mon, 18 Jul 2011 22:19:05 -0400 Subject: [rspec-users] Testing complex-form-example helpers In-Reply-To: References: Message-ID: Replies inline, below. On Mon, Jul 18, 2011 at 8:20 PM, Derek Prior wrote: >> One of the patterns I am trying to >> implement is from Ryan Bates' complex-form-examples[2]. Ryan's example >> doesn't have any tests, so I am trying to write tests for the code. > > He's updated that with a later episode series. Search his site for "nested" > and it's the first hit, I believe. I've checked this, and I believe his github project to which I referred uses the latest material from the railscast. >> I am starting with a helper function that he wrote, which makes it >> easy to add fields for an association into a form. The method does a >> little reflection, renders a fields_for call into a string, and then >> returns a link_to_function which calls a custom javascript function >> that adds the fields to the form. > > The realization I'm coming to is that a helper spec isn't necessarily the > best thing to cover this type of thing. RSpec is about specifying software > behavior. The behavior you want to test is, if I recall correctly from the > episode, that when the user clicks a link in the page, new association > fields should be added to the form.?I would likely skip the helper spec and > test this with a request spec or cucumber scenario using capybara & > selenium. I think this is a very good point. Before I dipped from my cucumber scenario directly into my helper spec, I should have written a request spec, or alternatively made my cucumber scenario more fleshed out. Perhaps what led me down the wrong path was knowing the code I needed to have before I had any tests. Since this is an educational project and there is no deadline to speak of, I have been very strict trying to write the minimal amount of code to make my tests pass, and thus I have very literal tests. In real life, I don't think that is always possible?how would we every write e-mail validators??but maybe I am over emphasizing it, still. Thoughts? From jed.schneider at gmail.com Wed Jul 20 12:29:53 2011 From: jed.schneider at gmail.com (Jed Schneider) Date: Wed, 20 Jul 2011 12:29:53 -0400 Subject: [rspec-users] eq or == which is the preferred idiom? Message-ID: for comparing objects properties, which notation is considered most idiomatic and best practice? Thanks, Jed Schneider -------------- next part -------------- An HTML attachment was scrubbed... URL: From dchelimsky at gmail.com Wed Jul 20 13:11:56 2011 From: dchelimsky at gmail.com (David Chelimsky) Date: Wed, 20 Jul 2011 12:11:56 -0500 Subject: [rspec-users] eq or == which is the preferred idiom? In-Reply-To: References: Message-ID: <9EB38035-695B-44C6-BD47-41D53BD585BA@gmail.com> On Jul 20, 2011, at 11:29 AM, Jed Schneider wrote: > for comparing objects properties, which notation is considered most idiomatic and best practice? Traditionally it was ==, but Ruby emits a warning. I've personally been moving toward eq, and if I had it to do over again that's all that would be there. I have no intention, however, of deprecating == as there is a wealth of code out there that uses it and users who prefer it in spite of the warning from Ruby. HTH, David From jko170 at gmail.com Wed Jul 20 13:18:01 2011 From: jko170 at gmail.com (Justin Ko) Date: Wed, 20 Jul 2011 11:18:01 -0600 Subject: [rspec-users] eq or == which is the preferred idiom? In-Reply-To: References: Message-ID: On Wed, Jul 20, 2011 at 10:29 AM, Jed Schneider wrote: > for comparing objects properties, which notation is considered most > idiomatic and best practice? > > Thanks, > > Jed Schneider > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > `eq` uses `==` under the hood. `eq` is the idiomatic approach for two reasons I can think of: 1.) It is more readable 2.) It is part of the "equality" DSL: `eq`, `eql`, `equal`. More info here: http://relishapp.com/rspec/rspec-expectations/v/2-6/dir/built-in-matchers/equality-matchers -------------- next part -------------- An HTML attachment was scrubbed... URL: From matt at mattwynne.net Wed Jul 20 17:09:01 2011 From: matt at mattwynne.net (Matt Wynne) Date: Wed, 20 Jul 2011 22:09:01 +0100 Subject: [rspec-users] eq or == which is the preferred idiom? In-Reply-To: References: Message-ID: <441D77FA-F51E-44D6-973C-9E3F991C2B0F@mattwynne.net> On 20 Jul 2011, at 18:18, Justin Ko wrote: > > > On Wed, Jul 20, 2011 at 10:29 AM, Jed Schneider wrote: > for comparing objects properties, which notation is considered most idiomatic and best practice? > > Thanks, > > Jed Schneider > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > > `eq` uses `==` under the hood. `eq` is the idiomatic approach for two reasons I can think of: > > 1.) It is more readable > 2.) It is part of the "equality" DSL: `eq`, `eql`, `equal`. > > More info here: http://relishapp.com/rspec/rspec-expectations/v/2-6/dir/built-in-matchers/equality-matchers I recently discovered eq, and one thing I like is that you can pass a custom failure message, as you could with a traditional assert: "foo".should eq("bar"), "what is wrong with your foo?" I expect this is also possible with == but it would look ugly. cheers, Matt -- Freelance programmer & coach Author, http://pragprog.com/books/hwcuc/the-cucumber-book (with Aslak Helles?y) Founder, http://relishapp.com +44(0)7974430184 | http://twitter.com/mattwynne -------------- next part -------------- An HTML attachment was scrubbed... URL: From patrick at collinatorstudios.com Thu Jul 21 15:49:03 2011 From: patrick at collinatorstudios.com (Patrick J. Collins) Date: Thu, 21 Jul 2011 12:49:03 -0700 (PDT) Subject: [rspec-users] question regarding how to test posting form data with Net::HTTP Message-ID: So I have a class that does this: def perform @response = Net::HTTP.new(url.host, url.port).start {|http| http.request(do_post) } unless @response.is_a?(Net::HTTPSuccess) || @response.is_a?(Net::HTTPRedirection) @response.error! end end def do_post Net::HTTP::Post.new(url.request_uri).tap do |req| req.set_form_data({:foo => "Bar") end end def url "http://myurl.com/something" end ... My question is-- Is there anything worth testing here? Since this is involving a 3rd party API, I shouldn't perform an actual post (for many reasons)... On the other hand, this is pretty much all Net::HTTP functionality so I should be able to rely on it just working as is. The only thing I can see in this that MIGHT be worth testing is that @response raises an error if it's not a HTTPSuccess or HTTPRedirection, but--- is that really worth doing? Thank you in advance for your input. Patrick J. Collins http://collinatorstudios.com From ckponnappa at gmail.com Thu Jul 21 16:50:27 2011 From: ckponnappa at gmail.com (Sidu Ponnappa) Date: Fri, 22 Jul 2011 02:20:27 +0530 Subject: [rspec-users] question regarding how to test posting form data with Net::HTTP In-Reply-To: References: Message-ID: Hi Patrick, I'd spec out the possible consequences of the different response codes as well what should happen in the event of a HTTP timeout. The requests themselves I would mock out because, as you pointed out, you're calling a 3rd party and you don't want live HTTP calls in your unit tests. Best, Sidu. http://sidu.in#code http://goldberg.c42.in On 22 July 2011 01:19, Patrick J. Collins wrote: > So I have a class that does this: > > def perform > @response = Net::HTTP.new(url.host, url.port).start {|http| > http.request(do_post) } > > unless @response.is_a?(Net::HTTPSuccess) || > @response.is_a?(Net::HTTPRedirection) > @response.error! > end > end > > def do_post > Net::HTTP::Post.new(url.request_uri).tap do |req| > req.set_form_data({:foo => "Bar") > end > end > > def url > "http://myurl.com/something" > end > ... > > My question is-- Is there anything worth testing here? Since this is > involving a 3rd party API, I shouldn't perform an actual post (for many > reasons)... On the other hand, this is pretty much all Net::HTTP > functionality > so I should be able to rely on it just working as is. > > The only thing I can see in this that MIGHT be worth testing is that > @response > raises an error if it's not a HTTPSuccess or HTTPRedirection, but--- is > that > really worth doing? > > Thank you in advance for your input. > > > Patrick J. Collins > http://collinatorstudios.com > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > -------------- next part -------------- An HTML attachment was scrubbed... URL: From ks at cissor.com Thu Jul 21 15:20:04 2011 From: ks at cissor.com (Byofuel) Date: Thu, 21 Jul 2011 12:20:04 -0700 (PDT) Subject: [rspec-users] RSpec fails with: No such file to load -- iconv (LoadError) Message-ID: <1181365.1762.1311276004469.JavaMail.geo-discussion-forums@prfr18> After reinstalling Ree 2011.03 (because of a readline dependency problem), RSpec no longer runs (although I can start the server without a problem). I get this error: /Users/u/.rvm/gems/ree-1.8.7-2011.03 at rails3/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:304:in `depend_on': No such file to load -- iconv (LoadError) Further down the stack trace, this is called from from /Users/u/code/app_name/config/environment.rb:5 which is this line: AppName::Application.initialize! So far, I've tried using RVM to install the iconv package, and then reinstalling Ree using the --with-iconv-dir switch to point Ree to the location of the iconv package. This did not make a difference. Note that in IRB, require 'iconv' returns true, and as mentioned, the server runs fine. Any suggestions? -------------- next part -------------- An HTML attachment was scrubbed... URL: From ks at cissor.com Fri Jul 22 00:00:46 2011 From: ks at cissor.com (Byofuel) Date: Thu, 21 Jul 2011 21:00:46 -0700 (PDT) Subject: [rspec-users] RSpec fails with: No such file to load -- iconv (LoadError) In-Reply-To: <1181365.1762.1311276004469.JavaMail.geo-discussion-forums@prfr18> Message-ID: <33384735.408.1311307246400.JavaMail.geo-discussion-forums@prln29> This problem was resolved after discovering that Ree had not installed with the correct path to iconv although it appeared to be specified correctly in the switch. After yet another reinstall, the error went away. -------------- next part -------------- An HTML attachment was scrubbed... URL: From jarmo.p at gmail.com Fri Jul 22 09:23:38 2011 From: jarmo.p at gmail.com (Jarmo Pertman) Date: Fri, 22 Jul 2011 06:23:38 -0700 (PDT) Subject: [rspec-users] how to modify RSpec.configuration correctly depending of the example groups Message-ID: Hi! I have a directory structure something similar to: spec/a/a_helper.rb spec/a/a_spec.rb spec/b/b_spec.rb In a_helper.rb i'd like to do something like this: RSpec.configure do |config| config.before :each do # do something for every example under "a" directory end end It works okay if i run: rspec spec/a or rspec spec/b If however i'd like to run all specs, then i get the following deprecation notice: ***************************************************************** DEPRECATION WARNING: you are using deprecated behaviour that will be removed from RSpec 3. You have set some configuration options after an example group has already been defined. In RSpec 3, this will not be allowed. All configuration should happen before the first example group is defined. The configuration is happening at: ./spec/a/a_helper.rb:2 ***************************************************************** How would i approach this problem differently where i'd like to set up some global before :each block for all the examples under directory "a", but doesn't want to run that block for all other examples? Is there any meaningful way to solve that problem without any need to write code for every example group under directory "a"? Jarmo From dchelimsky at gmail.com Fri Jul 22 09:27:12 2011 From: dchelimsky at gmail.com (David Chelimsky) Date: Fri, 22 Jul 2011 08:27:12 -0500 Subject: [rspec-users] how to modify RSpec.configuration correctly depending of the example groups In-Reply-To: References: Message-ID: <1B8D893F-C54F-4E19-B57E-E13B719786B6@gmail.com> On Jul 22, 2011, at 8:23 AM, Jarmo Pertman wrote: > Hi! > > I have a directory structure something similar to: > spec/a/a_helper.rb > spec/a/a_spec.rb > spec/b/b_spec.rb > > In a_helper.rb i'd like to do something like this: > RSpec.configure do |config| > config.before :each do > # do something for every example under "a" directory > end > end > > It works okay if i run: > rspec spec/a > > or > > rspec spec/b > > > If however i'd like to run all specs, then i get the following > deprecation notice: > ***************************************************************** > DEPRECATION WARNING: you are using deprecated behaviour that will > be removed from RSpec 3. > > You have set some configuration options after an example group has > already been defined. In RSpec 3, this will not be allowed. All > configuration should happen before the first example group is > defined. The configuration is happening at: > > ./spec/a/a_helper.rb:2 > ***************************************************************** > > > How would i approach this problem differently where i'd like to set up > some global before :each block for all the examples under directory > "a", but doesn't want to run that block for all other examples? Is > there any meaningful way to solve that problem without any need to > write code for every example group under directory "a"? > > Jarmo Take a look at how we do it in rspec-rails: https://github.com/rspec/rspec-rails/blob/master/lib/rspec/rails/example.rb#L10-38 From pablolmiranda at gmail.com Mon Jul 25 10:16:05 2011 From: pablolmiranda at gmail.com (Pablo L. de Miranda) Date: Mon, 25 Jul 2011 11:16:05 -0300 Subject: [rspec-users] Adding a folder to my rspec execution path Message-ID: Hi guys, I'm using RSpec with Capybara, and for that I created the spec/integration folder. But when I execute the command "bundle exec rspec spec" RSpec don't execute my files on integration folder. I need to pass each file to solve this problem. Do you known how I can put the integration folder on rspec execution path without need to pass each file? Thanks, Pablo From jko170 at gmail.com Mon Jul 25 10:27:12 2011 From: jko170 at gmail.com (Justin Ko) Date: Mon, 25 Jul 2011 08:27:12 -0600 Subject: [rspec-users] Adding a folder to my rspec execution path In-Reply-To: References: Message-ID: On Mon, Jul 25, 2011 at 8:16 AM, Pablo L. de Miranda < pablolmiranda at gmail.com> wrote: > Hi guys, > > I'm using RSpec with Capybara, and for that I created the > spec/integration folder. But when I execute the command "bundle exec > rspec spec" RSpec don't execute my files on integration folder. I need > to pass each file to solve this problem. > Do you known how I can put the integration folder on rspec execution > path without need to pass each file? > > Thanks, > > Pablo > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users "requests" and "integration" directories should be in the execution path. What version of rspec_rails are you using? -------------- next part -------------- An HTML attachment was scrubbed... URL: From pablolmiranda at gmail.com Mon Jul 25 11:03:52 2011 From: pablolmiranda at gmail.com (Pablo L. de Miranda) Date: Mon, 25 Jul 2011 12:03:52 -0300 Subject: [rspec-users] Adding a folder to my rspec execution path In-Reply-To: References: Message-ID: Hi Justin, I'm using RSpec 2.6.0, RSpec-Rails 2.6.1, Capybara 1.0.0 and Rails 3.0.7. Att, Pablo From jko170 at gmail.com Mon Jul 25 11:22:59 2011 From: jko170 at gmail.com (Justin Ko) Date: Mon, 25 Jul 2011 09:22:59 -0600 Subject: [rspec-users] Adding a folder to my rspec execution path In-Reply-To: References: Message-ID: On Mon, Jul 25, 2011 at 9:03 AM, Pablo L. de Miranda < pablolmiranda at gmail.com> wrote: > Hi Justin, > > I'm using RSpec 2.6.0, RSpec-Rails 2.6.1, Capybara 1.0.0 and Rails 3.0.7. > > Att, > > Pablo > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > "rspec spec" should definitely be running the specs in your integration folder. Just to be sure, is your directory structure like this? rails_root/spec/integration And you are running "rspec spec" from the rails root directory? -------------- next part -------------- An HTML attachment was scrubbed... URL: From pablolmiranda at gmail.com Mon Jul 25 11:59:28 2011 From: pablolmiranda at gmail.com (Pablo L. de Miranda) Date: Mon, 25 Jul 2011 12:59:28 -0300 Subject: [rspec-users] Adding a folder to my rspec execution path In-Reply-To: References: Message-ID: Justin, Bellow is my directory tree for spec: |-- spec | |-- controllers | | `-- site_controller_spec.rb | |-- factories.rb | |-- fixtures | | `-- avatar.jpg | |-- helpers | | |-- profile_helper_spec.rb | | |-- site_helper_spec.rb | | `-- task_list_helper_spec.rb | |-- integration | | |-- todo_list.rb | | |-- user_login.rb | | `-- user_profile.rb | |-- models | | |-- relationship_spec.rb | | |-- todo_list_item_spec.rb | | |-- todo_list_spec.rb | | `-- user_spec.rb | |-- spec_helper.rb | `-- support | `-- integration_example_group.rb And yes, I'm executing the rspec inside my rails root dir. I'm believing that the rspec will execute my integration tests without any modification on path, how I could add a dir on path executing? Thanks, Pablo From jko170 at gmail.com Mon Jul 25 12:32:11 2011 From: jko170 at gmail.com (Justin Ko) Date: Mon, 25 Jul 2011 10:32:11 -0600 Subject: [rspec-users] Adding a folder to my rspec execution path In-Reply-To: References: Message-ID: On Mon, Jul 25, 2011 at 9:59 AM, Pablo L. de Miranda < pablolmiranda at gmail.com> wrote: > Justin, > > Bellow is my directory tree for spec: > > |-- spec > | |-- controllers > | | `-- site_controller_spec.rb > | |-- factories.rb > | |-- fixtures > | | `-- avatar.jpg > | |-- helpers > | | |-- profile_helper_spec.rb > | | |-- site_helper_spec.rb > | | `-- task_list_helper_spec.rb > | |-- integration > | | |-- todo_list.rb > | | |-- user_login.rb > | | `-- user_profile.rb > | |-- models > | | |-- relationship_spec.rb > | | |-- todo_list_item_spec.rb > | | |-- todo_list_spec.rb > | | `-- user_spec.rb > | |-- spec_helper.rb > | `-- support > | `-- integration_example_group.rb > > And yes, I'm executing the rspec inside my rails root dir. > I'm believing that the rspec will execute my integration tests without > any modification on path, how I could add a dir on path executing? > > Thanks, > > Pablo > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > Ah ha! Your integration specs need to have _spec.rb in the file names, just like models, controllers, etc. -------------- next part -------------- An HTML attachment was scrubbed... URL: From dchelimsky at gmail.com Mon Jul 25 12:32:02 2011 From: dchelimsky at gmail.com (David Chelimsky) Date: Mon, 25 Jul 2011 11:32:02 -0500 Subject: [rspec-users] Adding a folder to my rspec execution path In-Reply-To: References: Message-ID: On Jul 25, 2011, at 10:59 AM, Pablo L. de Miranda wrote: > Justin, > > Bellow is my directory tree for spec: > > |-- spec > | |-- controllers > | | `-- site_controller_spec.rb > | |-- factories.rb > | |-- fixtures > | | `-- avatar.jpg > | |-- helpers > | | |-- profile_helper_spec.rb > | | |-- site_helper_spec.rb > | | `-- task_list_helper_spec.rb > | |-- integration > | | |-- todo_list.rb > | | |-- user_login.rb > | | `-- user_profile.rb These ^^ need to end in _spec.rb for the default settings to pick them up. You can configure that to work differently, but I'd recommend sticking w/ the convention. HTH, David > | |-- models > | | |-- relationship_spec.rb > | | |-- todo_list_item_spec.rb > | | |-- todo_list_spec.rb > | | `-- user_spec.rb > | |-- spec_helper.rb > | `-- support > | `-- integration_example_group.rb > > And yes, I'm executing the rspec inside my rails root dir. > I'm believing that the rspec will execute my integration tests without > any modification on path, how I could add a dir on path executing? > > Thanks, > > Pablo > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users From pablolmiranda at gmail.com Mon Jul 25 12:50:05 2011 From: pablolmiranda at gmail.com (Pablo L. de Miranda) Date: Mon, 25 Jul 2011 13:50:05 -0300 Subject: [rspec-users] Adding a folder to my rspec execution path In-Reply-To: References: Message-ID: OMG, I don't believe that I forget that. Thank you so much Justin. Att, Pablo From wilkerlucio at gmail.com Mon Jul 25 15:58:56 2011 From: wilkerlucio at gmail.com (Wilker) Date: Mon, 25 Jul 2011 16:58:56 -0300 Subject: [rspec-users] run rspec by hand (without spec command) Message-ID: Guys, I'm trying to do something a little exotic here, using RSpec to test Objective-C code through MacRuby. I mean the major problems I already solved (how to load Objective-C into ruby, install rspec for native macruby...), but in this case I just can't use the "spec" command, because the ruby command is dispatched directly by XCode... Im trying this: require "rubygems" require "rspec" require "rspec/autorun" RSpec.configure do |config| config.mock_with :rspec end # Loading all the Ruby project files. main = File.basename(__FILE__, File.extname(__FILE__)) dir_path = NSBundle.mainBundle.resourcePath.fileSystemRepresentation Dir.glob(File.join(dir_path, '*_spec.{rb,rbo}')).map { |x| File.basename(x, File.extname(x)) }.uniq.each do |path| if path != main require(path) end end But it's not working... this script just load the spec files using require, I meant it will work, but it don't... What Im missing in order to make specs to run after files are loaded? --- Wilker L?cio http://about.me/wilkerlucio/bio Kajabi Consultant +55 81 82556600 -------------- next part -------------- An HTML attachment was scrubbed... URL: From james at imaj.es Mon Jul 25 16:38:00 2011 From: james at imaj.es (James Cox) Date: Mon, 25 Jul 2011 16:38:00 -0400 Subject: [rspec-users] rspec and Lion? Message-ID: Hey, Has anyone used rspec and lion much yet? I'm using the Fuubar instafail formatter, and finding that the entire output of the spec run is buffered till the script execution stops, at which point it's displayed. Not sure why - it may be a failure/feature of the new Terminal.app ? Wondered if anyone had seen this? thanks, James From dchelimsky at gmail.com Mon Jul 25 21:40:35 2011 From: dchelimsky at gmail.com (David Chelimsky) Date: Mon, 25 Jul 2011 20:40:35 -0500 Subject: [rspec-users] rspec and Lion? In-Reply-To: References: Message-ID: <8BBC52E3-F3D1-428C-9AFA-450DB5F8A24A@gmail.com> On Jul 25, 2011, at 3:38 PM, James Cox wrote: > Hey, > > Has anyone used rspec and lion much yet? > > I'm using the Fuubar instafail formatter, and finding that the entire > output of the spec run is buffered till the script execution stops, at > which point it's displayed. Not sure why - it may be a failure/feature > of the new Terminal.app ? > > Wondered if anyone had seen this? I'm not seeing this at all. Anybody else? From chris at utilitygo.net Fri Jul 22 15:50:45 2011 From: chris at utilitygo.net (internetchris) Date: Fri, 22 Jul 2011 12:50:45 -0700 (PDT) Subject: [rspec-users] Keeping my integration specs DRY.. Message-ID: Hi Group, I'm finally taking the time to implement tests in some of my old apps. I'm using Rails 2.3.5, Rspec 1.3.1, Rspec-rails 1.3.3, and capybara. Here's an example of my first test, but I'm wondering how I setup the login routine so I can use it in future specs without repeating the method. Some sort of - before.each (:login) Can someone explain best practices for integration tests and if you're generous a simple code example. require 'spec_helper' describe "Users" do before(:each) do @user = Factory(:user) end it "login" do visit login_path fill_in :login, :with => @user.email fill_in :password, :with => @user.password click_button "login_button" page.has_content?("Logged in successfully") end it "creat profile" do visit new_social_profile_path end end From jko170 at gmail.com Tue Jul 26 00:37:35 2011 From: jko170 at gmail.com (Justin Ko) Date: Mon, 25 Jul 2011 22:37:35 -0600 Subject: [rspec-users] Keeping my integration specs DRY.. In-Reply-To: References: Message-ID: On Fri, Jul 22, 2011 at 1:50 PM, internetchris wrote: > Hi Group, > > I'm finally taking the time to implement tests in some of my old apps. > I'm using Rails 2.3.5, Rspec 1.3.1, Rspec-rails 1.3.3, and capybara. > > Here's an example of my first test, but I'm wondering how I setup the > login routine so I can use it in future specs without repeating the > method. Some sort of - before.each (:login) > > Can someone explain best practices for integration tests and if you're > generous a simple code example. > > > require 'spec_helper' > > describe "Users" do > before(:each) do > @user = Factory(:user) > end > > it "login" do > visit login_path > fill_in :login, :with => @user.email > fill_in :password, :with => @user.password > click_button "login_button" > page.has_content?("Logged in successfully") > end > > it "creat profile" do > visit new_social_profile_path > end > > end > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > Just put all of it in a method: def login(user) visit login_path fill_in :login, :with => user.email fill_in :password, :with => user.password click_button "login_button" end before(:each) { login } You can put the method in spec/support (and require it in spec_helper), or, define it in spec_helper -------------- next part -------------- An HTML attachment was scrubbed... URL: From patrick at collinatorstudios.com Tue Jul 26 01:51:38 2011 From: patrick at collinatorstudios.com (Patrick J. Collins) Date: Mon, 25 Jul 2011 22:51:38 -0700 (PDT) Subject: [rspec-users] Keeping my integration specs DRY.. In-Reply-To: References: Message-ID: > Here's an example of my first test, but I'm wondering how I setup the > login routine so I can use it in future specs without repeating the > method. Some sort of - before.each (:login) You can use shared examples... shared_examples_for "any page requiring authentication" do fill_in :login, :with => @user.email fill_in :password, :with => @user.password click_button "login_button" page.has_content?("Logged in successfully") end Then in your specs you can do: describe User do it_behaves_like "any page requiring authentication" end Patrick J. Collins http://collinatorstudios.com From dchelimsky at gmail.com Tue Jul 26 07:48:58 2011 From: dchelimsky at gmail.com (David Chelimsky) Date: Tue, 26 Jul 2011 06:48:58 -0500 Subject: [rspec-users] Keeping my integration specs DRY.. In-Reply-To: References: Message-ID: <48534FE3-C64F-402D-B984-D8E9BBCEFA9E@gmail.com> On Jul 26, 2011, at 12:51 AM, Patrick J. Collins wrote: >> Here's an example of my first test, but I'm wondering how I setup the >> login routine so I can use it in future specs without repeating the >> method. Some sort of - before.each (:login) > You can use shared examples... > > shared_examples_for "any page requiring authentication" do > fill_in :login, :with => @user.email > fill_in :password, :with => @user.password > click_button "login_button" > page.has_content?("Logged in successfully") > end > > Then in your specs you can do: > > describe User do > it_behaves_like "any page requiring authentication" > end You _can_ use shared content, but what you have here ^^ doesn't work as written. The scope in the shared_examples_for block is an example group scope, not an example scope, so methods like fill_in and click_button are not available. You'd want to wrap them in a before block. Also, page.has_content?(...) will return true or false, but will not act like an expectation (i.e. RSpec won't care which it returns). Here are a couple of ways you could do this that will work as Chris expects: shared_context "logged in" do let(:user) { Factory(:user) } before do fill_in :login, :with => user.email fill_in :password, :with => user.password click_button "login_button" end end describe ArticlesController do include_context "logged in" describe "GET edit" do it "does something" do # here the shared before hook has already run # and you have access to the `user` generated # by the `let` declaration. end end end This assumes you only want one user. It's simple and clean, without confusing indirection or parameterization. On the other extreme you could do this: shared_context "logged in" do before do visit new_session_path fill_in :login, :with => user.email fill_in :password, :with => user.password click_button :submit end end shared_context "admin" do let(:user) { Factory(:admin_user) } include_context "logged in" end shared_context "staff" do let(:user) { Factory(:staff_user) } include_context "logged in" end RSpec.configure do |conf| conf.alias_it_should_behave_like_to :logged_in_as end describe "Articles" do describe "GET index" do logged_in_as "admin" do it "does something" do # ... end end logged_in_as "staff" do it "does something else" do # ... end end end end The output from this looks like: ArticlesController GET index logged in as admin does something logged in as staff does something else Both of these approaches keep things explicit. You can also do a more implicit approach, like this: RSpec.configure do |c| c.before(:each, :type => :request) do @user = Factory(:user) visit new_session_path fill_in :login, :with => @user.email fill_in :password, :with => @user.password click_button :submit end end describe "Articles" do describe "GET index" do it "does something" do # ... end end end Here you don't see any evidence in the example that you're logging in. Less typing, but less clarity. I'd avoid this approach myself, but it is another option for you. HTH, David > Patrick J. Collins > http://collinatorstudios.com From apremdas at gmail.com Wed Jul 27 05:56:51 2011 From: apremdas at gmail.com (Andrew Premdas) Date: Wed, 27 Jul 2011 10:56:51 +0100 Subject: [rspec-users] --profile and ci Message-ID: Hi all I'd like to keep the 10 slowest results shown when using --profile as an artifact of my ci builds. Is there some way to direct this particular output to a file/folder? Is there anything that produces an html version of this output e.g. a formatter? I'd be interested to know why --profile is not a formatter (its not a formatter in I'm using Jenkins for Ci and rspec2, on a rails app TIA Andrew -- ------------------------ Andrew Premdas blog.andrew.premdas.org -------------- next part -------------- An HTML attachment was scrubbed... URL: From todd.sedano at sv.cmu.edu Wed Jul 27 13:09:49 2011 From: todd.sedano at sv.cmu.edu (Todd Sedano) Date: Wed, 27 Jul 2011 10:09:49 -0700 Subject: [rspec-users] Rspec with ActionMailer and .deliver Message-ID: I'm in the process of migrating from Rails 2 with rspec 1 to Rails 3 with rspec 2, the process has been going pretty well, however, today I came across an issue that I wanted to share. I have a controller that sends out an email through a mailer. Rails 2 code: CurriculumCommentMailer.deliver_comment_update(@curriculum_comment, "created") Rails 3 code: CurriculumCommentMailer.comment_update(@curriculum_comment, "created").deliver In my controller spec, I test to see if the email was sent out. Rspec 1 it "emails the comment" do CurriculumCommentMailer.should_receive(:deliver_comment_update) post :create, :curriculum_comment => @curriculum_comment.attributes end Rspec 2 In my opinion, I expected the following code to work it "emails the comment" do CurriculumCommentMailer.should_receive(:comment_update) post :create, :curriculum_comment => @curriculum_comment.attributes end However it does not. Since I'm calling should_receive on an object that isn't a stub/mock/double, I expected should_receive to call the underlying code, it does not so .deliver is called on a NilClass. (undefined method `deliver' for nil:NilClass) The following code does work mailer = double("mailer") mailer.stub(:deliver) CurriculumCommentMailer.should_receive(:comment_update).and_return(mailer) Whereas I expected this code to work, but it does not either CurriculumCommentMailer.should_receive(:comment_update).and_return(double("mailer").stub(:deliver)) thanks for your advice, Todd -------------- next part -------------- An HTML attachment was scrubbed... URL: From johnf at bitsbuilder.com Wed Jul 27 15:32:38 2011 From: johnf at bitsbuilder.com (John Feminella) Date: Wed, 27 Jul 2011 15:32:38 -0400 Subject: [rspec-users] Is there a "safe_stub"? Message-ID: Right now, #stub stubs a method even if it doesn't exist. Is there a way to stub a method on an object, if and only if it already #responds_to? that method? It would also be good if it raised an ArgumentError or the like. Example: # this is how it works now: # - stubs the #bar method even if it doesn't exist # - always returns 'baz' foo.stub(:bar).and_return('baz') What I'd like is something like this: # raise ArgumentError unless foo.respond_to? :bar # otherwise, behaves as the regular #stub does foo.stub!(:bar).and_return('baz') Thoughts? ~ jf -- John Feminella Principal Consultant, BitsBuilder LI: http://www.linkedin.com/in/johnxf SO: http://stackoverflow.com/users/75170/ From dchelimsky at gmail.com Wed Jul 27 16:05:33 2011 From: dchelimsky at gmail.com (David Chelimsky) Date: Wed, 27 Jul 2011 15:05:33 -0500 Subject: [rspec-users] Is there a "safe_stub"? In-Reply-To: References: Message-ID: <817760A5-9EE4-4474-B69C-E86BB11F72EE@gmail.com> On Jul 27, 2011, at 2:32 PM, John Feminella wrote: > Right now, #stub stubs a method even if it doesn't exist. Is there a > way to stub a method on an object, if and only if it already > #responds_to? that method? It would also be good if it raised an > ArgumentError or the like. > > Example: > > # this is how it works now: > # - stubs the #bar method even if it doesn't exist > # - always returns 'baz' > foo.stub(:bar).and_return('baz') > > What I'd like is something like this: > > # raise ArgumentError unless foo.respond_to? :bar > # otherwise, behaves as the regular #stub does > foo.stub!(:bar).and_return('baz') > > Thoughts? This has come up many times before. Please check out http://groups.google.com/group/rspec/browse_thread/thread/c344c898c278cd2b and https://github.com/rspec/rspec-mocks/issues/15 to see where the conversation is at this point. From johnf at bitsbuilder.com Wed Jul 27 18:36:13 2011 From: johnf at bitsbuilder.com (John Feminella) Date: Wed, 27 Jul 2011 18:36:13 -0400 Subject: [rspec-users] Is there a "safe_stub"? In-Reply-To: <817760A5-9EE4-4474-B69C-E86BB11F72EE@gmail.com> References: <817760A5-9EE4-4474-B69C-E86BB11F72EE@gmail.com> Message-ID: Apologies for not noticing this -- I did search, but I wasn't sure exactly how to phrase it. Thanks for the info, David. ~ jf -- John Feminella Principal Consultant, BitsBuilder LI: http://www.linkedin.com/in/johnxf SO: http://stackoverflow.com/users/75170/ On Wed, Jul 27, 2011 at 16:05, David Chelimsky wrote: > On Jul 27, 2011, at 2:32 PM, John Feminella wrote: > >> Right now, #stub stubs a method even if it doesn't exist. Is there a >> way to stub a method on an object, if and only if it already >> #responds_to? that method? It would also be good if it raised an >> ArgumentError or the like. >> >> Example: >> >> # this is how it works now: >> # ? - stubs the #bar method even if it doesn't exist >> # ? - always returns 'baz' >> foo.stub(:bar).and_return('baz') >> >> What I'd like is something like this: >> >> # raise ArgumentError unless foo.respond_to? :bar >> # otherwise, behaves as the regular #stub does >> foo.stub!(:bar).and_return('baz') >> >> Thoughts? > > This has come up many times before. Please check out http://groups.google.com/group/rspec/browse_thread/thread/c344c898c278cd2b and https://github.com/rspec/rspec-mocks/issues/15 to see where the conversation is at this point. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > From dchelimsky at gmail.com Wed Jul 27 20:29:50 2011 From: dchelimsky at gmail.com (David Chelimsky) Date: Wed, 27 Jul 2011 19:29:50 -0500 Subject: [rspec-users] Is there a "safe_stub"? In-Reply-To: References: <817760A5-9EE4-4474-B69C-E86BB11F72EE@gmail.com> Message-ID: <10B8511C-4393-47E9-9A5E-2354942F13CD@gmail.com> On Jul 27, 2011, at 5:36 PM, John Feminella wrote: > Apologies for not noticing this -- I did search, but I wasn't sure > exactly how to phrase it. Thanks for the info, David. No apology necessary. Just saving myself a bunch of re-typing :) Feel free to weigh in on those threads. Cheers, David > ~ jf > -- > John Feminella > Principal Consultant, BitsBuilder > LI: http://www.linkedin.com/in/johnxf > SO: http://stackoverflow.com/users/75170/ > > > > On Wed, Jul 27, 2011 at 16:05, David Chelimsky wrote: >> On Jul 27, 2011, at 2:32 PM, John Feminella wrote: >> >>> Right now, #stub stubs a method even if it doesn't exist. Is there a >>> way to stub a method on an object, if and only if it already >>> #responds_to? that method? It would also be good if it raised an >>> ArgumentError or the like. >>> >>> Example: >>> >>> # this is how it works now: >>> # - stubs the #bar method even if it doesn't exist >>> # - always returns 'baz' >>> foo.stub(:bar).and_return('baz') >>> >>> What I'd like is something like this: >>> >>> # raise ArgumentError unless foo.respond_to? :bar >>> # otherwise, behaves as the regular #stub does >>> foo.stub!(:bar).and_return('baz') >>> >>> Thoughts? >> >> This has come up many times before. Please check out http://groups.google.com/group/rspec/browse_thread/thread/c344c898c278cd2b and https://github.com/rspec/rspec-mocks/issues/15 to see where the conversation is at this point. >> _______________________________________________ >> From piter.fcbk at gmail.com Thu Jul 28 11:22:57 2011 From: piter.fcbk at gmail.com (Piter Fcbk) Date: Thu, 28 Jul 2011 12:22:57 -0300 Subject: [rspec-users] RSpec, shoulda-matchers and Rails model attributes validations Message-ID: I was trying out RSpec framework in a project and got stopped doing the unit test of a model. In particular, doing the test for the associations and the ActiveRecord validations. I started writing the validations but my tests didn't look DRY at all. Before refactoring the tests checked out and look for other people solutions. I found out shoulda-matchers and Shoulda (which if I didn't get it wrong is another testing framework). Actually I found another one, remarkable, but it look it doesn't work with Rails 3. Does anyone have any advice, comments, suggestion on this matter? Right now I continue on using RSpec and shoulda-matchers. The last one mainly for testing the validations and associations of the models. Any advice and/or help will be appreciated. Thanks in advance. -------------- next part -------------- An HTML attachment was scrubbed... URL: From zach.dennis at gmail.com Thu Jul 28 11:38:36 2011 From: zach.dennis at gmail.com (Zach Dennis) Date: Thu, 28 Jul 2011 11:38:36 -0400 Subject: [rspec-users] Looking for a fellow human: Agile, Mobile, Web, Rails Developer Message-ID: Not to spam the list, but I thought the RSpec and Cucumber communities may be appropriate to tap into. We're looking for agilist web/mobile developer(s) over at Mutually Human. RSpec/Cuke creds hold special places in our hearts. http://mutuallyhuman.com/blog/2011/07/28/looking-for-a-fellow-human-agile-mobile-web-rails-developer -- Zach Dennis http://www.continuousthinking.com (personal) http://www.mutuallyhuman.com (hire me) http://ideafoundry.info/behavior-driven-development (first rate BDD training) @zachdennis (twitter) -------------- next part -------------- An HTML attachment was scrubbed... URL: From dchelimsky at gmail.com Thu Jul 28 12:01:28 2011 From: dchelimsky at gmail.com (David Chelimsky) Date: Thu, 28 Jul 2011 11:01:28 -0500 Subject: [rspec-users] RSpec, shoulda-matchers and Rails model attributes validations In-Reply-To: References: Message-ID: <7B1B119C-B3FF-412E-8EC8-B455E7D65C5A@gmail.com> On Jul 28, 2011, at 10:22 AM, Piter Fcbk wrote: > I was trying out RSpec framework in a project and got stopped doing the unit test of a model. In particular, doing the test for the associations and the ActiveRecord validations. > I started writing the validations but my tests didn't look DRY at all. Before refactoring the tests checked out and look for other people solutions. > I found out shoulda-matchers and Shoulda (which if I didn't get it wrong is another testing framework). Actually I found another one, remarkable, but it look it doesn't work with Rails 3. > > Does anyone have any advice, comments, suggestion on this matter? > > Right now I continue on using RSpec and shoulda-matchers. The last one mainly for testing the validations and associations of the models. > Any advice and/or help will be appreciated. > > Thanks in advance. rspec + shoulda matchers is a fairly common pairing these days, so you're not alone. Personally, I use them for validations (which are behavior), but not for associations (which are structure). Rather than specifying, for example, that a team has many players, I just use the players collection in an example that uses them: describe Team do it "does not have any open spots if there are 30 players" do team = Factory(:team) 30.times { team.players << Factory(:player) } team.should_not have_opening end end HTH, David -------------- next part -------------- An HTML attachment was scrubbed... URL: From lists at ruby-forum.com Thu Jul 28 12:39:20 2011 From: lists at ruby-forum.com (Roger Pack) Date: Thu, 28 Jul 2011 18:39:20 +0200 Subject: [rspec-users] run rspec by hand (without spec command) In-Reply-To: References: Message-ID: <00858f94525b7c0a02e787a407cdd78d@ruby-forum.com> maybe it runs at at_exit? -- Posted via http://www.ruby-forum.com/. From lists at ruby-forum.com Thu Jul 28 12:40:28 2011 From: lists at ruby-forum.com (Roger Pack) Date: Thu, 28 Jul 2011 18:40:28 +0200 Subject: [rspec-users] !~ seems to not work Message-ID: <700ea9c843242075667e787caa55d21e@ruby-forum.com> >> 'abc' ~! /def/ => true 'abc'.should !~ /def/ fails though. Seemed unexpected... -roger- -- Posted via http://www.ruby-forum.com/. From dchelimsky at gmail.com Thu Jul 28 13:04:15 2011 From: dchelimsky at gmail.com (David Chelimsky) Date: Thu, 28 Jul 2011 12:04:15 -0500 Subject: [rspec-users] !~ seems to not work In-Reply-To: <700ea9c843242075667e787caa55d21e@ruby-forum.com> References: <700ea9c843242075667e787caa55d21e@ruby-forum.com> Message-ID: On Jul 28, 2011, at 11:40 AM, Roger Pack wrote: >>> 'abc' ~! /def/ > => true > > 'abc'.should !~ /def/ > > fails though. Seemed unexpected... > -roger- This comes up from time to time but it's a bitch to google for. It boils down to this: the only way to support "actual.should != unexpected" in Ruby 1.8 is to go back and parse the file. This is because == is a method but != is not a method: it's handled by the parser. What that means is this: 5.should == 5 # becomes 5.should.==(5) 5.should != 4 # becomes !(5.should.==(4)) In the latter case the code evaluating 5 == 4 has no way to know that it's been negated. HTH, David From samwgoldman at gmail.com Thu Jul 28 14:42:21 2011 From: samwgoldman at gmail.com (Sam Goldman) Date: Thu, 28 Jul 2011 14:42:21 -0400 Subject: [rspec-users] RSpec, shoulda-matchers and Rails model attributes validations In-Reply-To: References: Message-ID: I think that there is a lot of value to projects like shoulda-matchers (which is separate from shoulda, the testing framework) and remarkable. The idea is that you should not be testing the framework (rails). That is, you should not be testing that defining a has_many association creates a method on your model. You still need to test all of your domain logic, which David's example shows. Also, remarkable does work in rails 3. I am using it because it has a matcher for accepts_nested_attributes_for, while shoulda-matchers does not. Using this line in my Gemfile works: gem 'remarkable_activerecord', '~> 4.0.0.alpha2' Sam On Thu, Jul 28, 2011 at 11:22 AM, Piter Fcbk wrote: > I was trying out RSpec framework in a project and got stopped doing the unit > test of a model. In particular, doing the test for the associations and the > ActiveRecord validations. > I started writing the validations but my tests didn't look DRY at all. > Before refactoring the tests checked out and look for other people > solutions. > I found out shoulda-matchers and Shoulda (which if I didn't get it wrong is > another testing framework). Actually I found another one, remarkable, but it > look it doesn't work with Rails 3. > Does anyone have any advice, comments, suggestion on this matter? > Right now I continue on using RSpec and shoulda-matchers. The last one > mainly for testing the validations and associations of the models. > Any advice and/or help will be appreciated. > Thanks in advance. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > From rick.denatale at gmail.com Fri Jul 29 07:04:09 2011 From: rick.denatale at gmail.com (Rick DeNatale) Date: Fri, 29 Jul 2011 07:04:09 -0400 Subject: [rspec-users] !~ seems to not work In-Reply-To: References: <700ea9c843242075667e787caa55d21e@ruby-forum.com> Message-ID: On Thu, Jul 28, 2011 at 1:04 PM, David Chelimsky wrote: > On Jul 28, 2011, at 11:40 AM, Roger Pack wrote: > > >>> 'abc' ~! /def/ > > => true > > > > 'abc'.should !~ /def/ > > > > fails though. Seemed unexpected... > > -roger- > > This comes up from time to time but it's a bitch to google for. It boils > down to this: the only way to support "actual.should != unexpected" in Ruby > 1.8 is to go back and parse the file. This is because == is a method but != > is not a method: it's handled by the parser. What that means is this: > > 5.should == 5 > # becomes > 5.should.==(5) > > 5.should != 4 > # becomes > !(5.should.==(4)) > > In the latter case the code evaluating 5 == 4 has no way to know that it's > been negated. > > HTH, > David > All true David, but you might want to get your eyeglass prescription checked, or maybe you glossed over the difference between = and ~. Roger is using != but !~ which is the negated form of ~=, although I think !~ uses the same kind of compile time expansion as !=. And then, Roger is trying to transpose the characters to ~! which is syntactically incorrect sugar. Although this might be a typo in the post. ? irb >> 'abc' !~ /def/ => true >> 'abc' ~! /def/ SyntaxError: compile error (irb):2: syntax error, unexpected '~', expecting $end 'abc' ~! /def/ ^ from (irb):2 And of course the solution is to use should_not 'abc'.should_not =~ /def/ or 'abc'.should_not match(/def/) -- Rick DeNatale Blog: http://talklikeaduck.denhaven2.com/ Github: http://github.com/rubyredrick Twitter: @RickDeNatale WWR: http://www.workingwithrails.com/person/9021-rick-denatale LinkedIn: http://www.linkedin.com/in/rickdenatale -------------- next part -------------- An HTML attachment was scrubbed... URL: From dchelimsky at gmail.com Fri Jul 29 07:59:42 2011 From: dchelimsky at gmail.com (David Chelimsky) Date: Fri, 29 Jul 2011 06:59:42 -0500 Subject: [rspec-users] !~ seems to not work In-Reply-To: References: <700ea9c843242075667e787caa55d21e@ruby-forum.com> Message-ID: <2D57C49E-B2ED-4754-9EB7-62F3DB4D55C3@gmail.com> On Jul 29, 2011, at 6:04 AM, Rick DeNatale wrote: > On Thu, Jul 28, 2011 at 1:04 PM, David Chelimsky wrote: > On Jul 28, 2011, at 11:40 AM, Roger Pack wrote: > > >>> 'abc' ~! /def/ > > => true > > > > 'abc'.should !~ /def/ > > > > fails though. Seemed unexpected... > > -roger- > > This comes up from time to time but it's a bitch to google for. It boils down to this: the only way to support "actual.should != unexpected" in Ruby 1.8 is to go back and parse the file. This is because == is a method but != is not a method: it's handled by the parser. What that means is this: > > 5.should == 5 > # becomes > 5.should.==(5) > > 5.should != 4 > # becomes > !(5.should.==(4)) > > In the latter case the code evaluating 5 == 4 has no way to know that it's been negated. > > HTH, > David > > All true David, but you might want to get your eyeglass prescription checked, or maybe you glossed over the difference between = and ~. Probably a bit of both :) > Roger is using != but !~ which is the negated form of ~=, although I think !~ uses the same kind of compile time expansion as !=. That appears to be the case. > And then, Roger is trying to transpose the characters to ~! which is syntactically incorrect sugar. Although this might be a typo in the post. > > ? irb > >> 'abc' !~ /def/ > => true > >> 'abc' ~! /def/ > SyntaxError: compile error > (irb):2: syntax error, unexpected '~', expecting $end > 'abc' ~! /def/ > ^ > from (irb):2 > > > > And of course the solution is to use should_not > > 'abc'.should_not =~ /def/ > or > 'abc'.should_not match(/def/) I prefer the latter, which leads me to prefer 'abc'.should match(/bc/) as well. Thanks for setting things straight. Cheers, David > -- > Rick DeNatale > > Blog: http://talklikeaduck.denhaven2.com/ > Github: http://github.com/rubyredrick > Twitter: @RickDeNatale > WWR: http://www.workingwithrails.com/person/9021-rick-denatale > LinkedIn: http://www.linkedin.com/in/rickdenatale -------------- next part -------------- An HTML attachment was scrubbed... URL: From rick.denatale at gmail.com Fri Jul 29 09:20:27 2011 From: rick.denatale at gmail.com (Rick DeNatale) Date: Fri, 29 Jul 2011 09:20:27 -0400 Subject: [rspec-users] !~ seems to not work In-Reply-To: <2D57C49E-B2ED-4754-9EB7-62F3DB4D55C3@gmail.com> References: <700ea9c843242075667e787caa55d21e@ruby-forum.com> <2D57C49E-B2ED-4754-9EB7-62F3DB4D55C3@gmail.com> Message-ID: On Fri, Jul 29, 2011 at 7:59 AM, David Chelimsky wrote: > Thanks for setting things straight. Just happy to be here sir! -- Rick DeNatale Blog: http://talklikeaduck.denhaven2.com/ Github: http://github.com/rubyredrick Twitter: @RickDeNatale WWR: http://www.workingwithrails.com/person/9021-rick-denatale LinkedIn: http://www.linkedin.com/in/rickdenatale -------------- next part -------------- An HTML attachment was scrubbed... URL: From john.hinnegan at gmail.com Fri Jul 29 12:52:52 2011 From: john.hinnegan at gmail.com (John Hinnegan) Date: Fri, 29 Jul 2011 09:52:52 -0700 Subject: [rspec-users] does anyone's rspec actually exit on the first ^C? Message-ID: So, you start running your (full and long) rspec suite (before a checkin), and you realize you [forgot to migrate test|didn't add some config value|missed a curly brace]. You hit Ctrl+C and get this mesage Exiting... Interrupt again to exit immediately. Then the tests march on. My question is, does anyone's rspec actually exit after that? I invariably have to hit Ctrl+C a second time to get it to exit. If I don't, rspec will finish the suite. It does not appear to be exiting. I guess I'm asking if this is abnormal? Will rspec actually exit on its own on some conditions after the first Ctrl+C? -------------- next part -------------- An HTML attachment was scrubbed... URL: From dchelimsky at gmail.com Fri Jul 29 13:28:26 2011 From: dchelimsky at gmail.com (David Chelimsky) Date: Fri, 29 Jul 2011 12:28:26 -0500 Subject: [rspec-users] does anyone's rspec actually exit on the first ^C? In-Reply-To: References: Message-ID: On Jul 29, 2011, at 11:52 AM, John Hinnegan wrote: > So, you start running your (full and long) rspec suite (before a checkin), and you realize you [forgot to migrate test|didn't add some config value|missed a curly brace]. > > You hit Ctrl+C and get this mesage > Exiting... Interrupt again to exit immediately. > Then the tests march on. When you hit CTRL-C the first time, it removes all examples that haven't yet run from the queue, but lets the currently running example finish out. That way all the after hooks run so files are cleaned up, transactions rolled back, etc. i.e. you leave the system in a good state. The 2nd time it just exits, but then you risk leaving things in a bad state. Are you sure it's running more examples after the initial CTRL-C? Is it possible that you're just hitting it on a long-running example? > My question is, does anyone's rspec actually exit after that? I invariably have to hit Ctrl+C a second time to get it to exit. If I don't, rspec will finish the suite. It does not appear to be exiting. > > I guess I'm asking if this is abnormal? Will rspec actually exit on its own on some conditions after the first Ctrl+C? From john.hinnegan at gmail.com Fri Jul 29 13:36:53 2011 From: john.hinnegan at gmail.com (John Hinnegan) Date: Fri, 29 Jul 2011 10:36:53 -0700 Subject: [rspec-users] How can I make before/after all a method? Message-ID: I would like to turn this: describe TestClass do before :all do # set some config end after :all do # restore some config end # do a bunch of tests to this end into describe TestClass do with_config_value(X) # do a bunch of tests to this end Basically, I want to include before :all and after :all clauses into a test. However, I do not want this to apply toa ll tests. Thanks in advance. -------------- next part -------------- An HTML attachment was scrubbed... URL: From dchelimsky at gmail.com Fri Jul 29 15:58:06 2011 From: dchelimsky at gmail.com (David Chelimsky) Date: Fri, 29 Jul 2011 14:58:06 -0500 Subject: [rspec-users] How can I make before/after all a method? In-Reply-To: References: Message-ID: On Jul 29, 2011, at 12:36 PM, John Hinnegan wrote: > I would like to turn this: > > describe TestClass do > before :all do > # set some config > end > after :all do > # restore some config > end > # do a bunch of tests to this > end > > into > > describe TestClass do > with_config_value(X) > > # do a bunch of tests to this > end > > > Basically, I want to include before :all and after :all clauses into a test. However, I do not want this to apply toa ll tests. > > Thanks in advance. There a number of ways to do this, but if you really just want a method, then just write one! def with_config_value(foo) before(:all) do # stuff that can access foo end after(:all) do # more stuff that can use foo end end HTH, David From matt at mattwynne.net Fri Jul 29 16:24:35 2011 From: matt at mattwynne.net (Matt Wynne) Date: Fri, 29 Jul 2011 21:24:35 +0100 Subject: [rspec-users] How can I make before/after all a method? In-Reply-To: References: Message-ID: <31342BB3-C7B6-44A9-AF48-5F84B0B3C3D9@mattwynne.net> On 29 Jul 2011, at 18:36, John Hinnegan wrote: > I would like to turn this: > > describe TestClass do > before :all do > # set some config > end > after :all do > # restore some config > end > # do a bunch of tests to this > end > > into > > describe TestClass do > with_config_value(X) > > # do a bunch of tests to this > end > > > Basically, I want to include before :all and after :all clauses into a test. However, I do not want this to apply toa ll tests. > > Thanks in advance. What you're talking about is generally called a macro. describe TestClass do def self.with_config_value(config) before :all do #set some config end #etc. end with_config_value(x) do # do a bunch of tests end end You can put the macro in a module and either use #extend to make it available in a describe block, or use RSpec's config.extend method to do that automatically. Does that help? cheers, Matt -- Freelance programmer & coach Author, http://pragprog.com/book/hwcuc/the-cucumber-book (with Aslak Helles?y) Founder, http://relishapp.com +44(0)7974430184 | http://twitter.com/mattwynne From dblockdotorg at gmail.com Sat Jul 30 16:07:54 2011 From: dblockdotorg at gmail.com (dblock) Date: Sat, 30 Jul 2011 13:07:54 -0700 (PDT) Subject: [rspec-users] How can I add another 'requests'-like folder? Message-ID: I love conventions. But I am trying to write tests for an API,which lives in ... api. I need to be making http requests, so all my tests work when put in spec/requests. How can I make that spec/api? (Obviously renaming requests into api doesn't bring in the code that lets me do GETs). From dchelimsky at gmail.com Sat Jul 30 16:55:51 2011 From: dchelimsky at gmail.com (David Chelimsky) Date: Sat, 30 Jul 2011 15:55:51 -0500 Subject: [rspec-users] How can I add another 'requests'-like folder? In-Reply-To: References: Message-ID: On Jul 30, 2011, at 3:07 PM, dblock wrote: > I love conventions. But I am trying to write tests for an API,which > lives in ... api. I need to be making http requests, so all my tests > work when put in spec/requests. How can I make that spec/api? > (Obviously renaming requests into api doesn't bring in the code that > lets me do GETs). Take a look at https://github.com/rspec/rspec-rails/blob/master/lib/rspec/rails/example.rb. You can do something similar in spec_helper.rb: RSpec::configure do |c| c.include RSpec::Rails::RequestExampleGroup, :example_group => { :file_path => /spec\/api// } end From jko170 at gmail.com Sat Jul 30 18:02:11 2011 From: jko170 at gmail.com (Justin Ko) Date: Sat, 30 Jul 2011 16:02:11 -0600 Subject: [rspec-users] How can I add another 'requests'-like folder? In-Reply-To: References: Message-ID: On Sat, Jul 30, 2011 at 2:07 PM, dblock wrote: > I love conventions. But I am trying to write tests for an API,which > lives in ... api. I need to be making http requests, so all my tests > work when put in spec/requests. How can I make that spec/api? > (Obviously renaming requests into api doesn't bring in the code that > lets me do GETs). > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > Add this to your spec_helper.rb: c.include RSpec::Rails::RequestExampleGroup, :type => :request, :example_group => { :file_path => /spec\/api/ } -------------- next part -------------- An HTML attachment was scrubbed... URL: