Re: [rspec-users] How to test modules and custom validators
I recently wrote this spec https://gist.github.com/1126847 for a custom validator. I hope you find it useful. Feedback welcome. Craig ___ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users
Re: [rspec-users] [ANN] Welcome Chad, Pat, Justin, and Myron!
Welcome, everyone! Thanks for helping to provide all of us with high-quality, useful tools that help us do what we love. Regards, Craig ___ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users
Re: [rspec-users] Simple Association Redirect on delete
On Sun, Feb 27, 2011 at 10:57 PM, Charley charley.st...@gmail.com wrote: ... it redirects to the bugs list do Bug.stub(:find) { mock_bug } delete :destroy, :id = 1 response.should redirect_to(bugs_url) end This fails for obvious reasons. A bug belongs to a project so the redirect should go to the projects_url(someproject_id_I_dont_know) I am sure this is a fairly simple and common thing. Can anyone help put me on the right path? Make the mock_bug respond to project_id. Then verify that the request's response goes to project_path(:id = mock_bug.project_id). Regards, Craig ___ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users
Re: [rspec-users] Rspec2 for rails 2.3.8?
On Fri, Feb 25, 2011 at 4:30 PM, Radhesh Kamath li...@ruby-forum.comwrote: Which version of rspec-rails should I use for Rails 2.3.8? rspec-rails 1.3.3 is currently the latest release for Rails 2.3.x. Regards, Craig ___ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users
Re: [rspec-users] Validations aren't being performed when running 'rspec spec'
On Wed, Feb 23, 2011 at 10:29 AM, Tom Milewski tmilew...@gmail.com wrote: Model: validates_presence_of :name, :location, :email... Test: it should ensure that name is present do Factory.build(:public_agent, :name = nil).should have(1).error_on(:name) end All of these tests do not return errors and do add the invalid records to the database. Has anyone else had this happen? Suggestions? Though I'm still using RSpec 1, I do use FactoryGirl. Factory.build only instantiates a new record; it does not save it. Thus, the example that you shared should not create a record. Are you sure that the invalid records that you're seeing in the database are from this example? Regards, Craig ___ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users
Re: [rspec-users] Validations aren't being performed when running 'rspec spec'
On Thu, Feb 24, 2011 at 2:07 PM, Tom Milewski tmilew...@gmail.com wrote: Thanks for the replies. Here's the error I'm seeing when running all specs (.build and .create): Agent while creating should ensure that name is present Failure/Error: Factory(:public_agent, :name = nil).should have(1).error_on(:name) expected 1 error on :name, got 0 # ./spec/models/agent_spec.rb:35:in `block (3 levels) in top (required)' Here's what I'm seeing when running model specs with .create: Agent while creating should ensure that name is present Failure/Error: Factory(:public_agent, :name = nil).should have(1).error_on(:name) ActiveRecord::RecordInvalid: Validation failed: Name can't be blank # ./spec/models/agent_spec.rb:35:in `block (3 levels) in top (required)' Everything passes when running model specs with .build. Have you tried with Agent.new and/or Agent.create? Maybe factory_girl is doing something strange. Also, what versions of rails, rspec-rails, rspec and factory_girl are you using? Craig ___ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users
Re: [rspec-users] having some issues with factory_girl and bundler...
On Mon, Sep 27, 2010 at 5:05 PM, Justin Ko jko...@gmail.com wrote: On Sep 27, 4:11 pm, Carmen Díaz Echauri cdecha...@gmail.com wrote: Hi All Need some brilliants minds here :) I'm using Rails 2.3.8 ruby 1.8 (I know, I know... ) I've move my gems from .gems to Bundler, but I'm having some issues with Factory_girl when I rake spec uninitialized constant Factory ... Try: gem 'factory_girl_rails' According to http://github.com/thoughtbot/factory_girl, that's only for Rails 3: If you want to use factory_girl with Rails 3, see github.com/thoughtbot/factory_girl_rails. I've been working on moving a Rails 2.3.9 app to Bundler 1.0.0 from a custom solution. The app uses factory_girl with rspec and cucumber, and all the specs and cukes run successfully. At the bottom of config/environments/cucumber.rb, I have require 'factory_girl'. I don't have any similar requires in config/environments/test.rb or in spec/spec_helper.rb. Here's the current Gemfile: source http://rubygems.org; gem rails, ~ 2.3.9 gem rake, ~ 0.8.7 gem haml, ~ 3.0.18 gem fastercsv, ~ 1.5.3 gem will_paginate, ~ 2.3.12 gem rspec,~ 1.3.0, :group = [:test, :cucumber] gem rspec-rails, ~ 1.3.2, :group = [:test, :cucumber] gem capybara, ~ 0.3.8, :group = [:cucumber] gem cucumber, ~ 0.7.3, :group = [:cucumber] gem cucumber-rails, ~ 0.3.1, :group = [:cucumber] gem gherkin, ~ 1.0.30, :group = [:cucumber] gem database_cleaner, ~ 0.5.0, :group = [:cucumber] gem dupe, ~ 0.5.1, :group = [:test, :cucumber] gem factory_girl, ~ 1.2.4, :group = [:test, :cucumber] platforms :jruby do gem jruby-openssl, ~ 0.7.1 gem jruby-rack,~ 1.0.3 gem warbler, ~ 1.0.1 gem flying_saucer, ~ 0.8.0 group :db do gem jdbc-mysql, ~ 5.0.4 gem activerecord-jdbc-adapter, ~ 0.9.2 gem activerecord-jdbcmysql-adapter, ~ 0.9.2 end end Note that I'm not using the :require option with gem factory_girl. Since I don't have any ideas about your specific errors and I have to head out to an appointment, I hope all of this helps. Regards, Craig ___ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users
Re: [rspec-users] Trying to spec all models
On Thu, Sep 16, 2010 at 9:24 PM, Ed Howland ed.howl...@gmail.com wrote: Phillip, any updates on your efforts? I am keenly interested in a Cucumber tags like facility for RSpec Though I haven't been closely following RSpec 2, I think it's going to have tags much like Cucumber. Regards, Craig ___ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users
Re: [rspec-users] Missing template in helper specs with a render
On Tue, Aug 24, 2010 at 5:19 AM, Alex Pressberg li...@ruby-forum.comwrote: Yes! We do use render in helpers to a great extent and are bitten by the missing template in view path error too. Any workarounds? Can the view path be easily fixed for helper specs in Rspec 1.3? Does Rspec 2 already support this? If you're doing it, it's obviously possible, but it feels wrong to me. I think of a helper as a small piece of code that helps me decide what to render, gives me a CSS class name based on some condition, gives me a small chunk of markup whose generation would clutter up the view, etc. Have you considered that, though the framework allows you to do this, it may not be a good approach? In other words, instead of looking for a workaround, would it make more sense (at least in the long term) to change your application? I'm sorry I can't offer actual help w/ current tools. I'm curious if Rails 3 still supports this or if RSpec 2 will support it. Regards, Craig David Chelimsky wrote: On Thu, Jun 4, 2009 at 8:41 PM, Charlie Bowman charlesmbow...@gmail.com wrote: I have a helper method that does a render :partial.� The method works fine within the app (Rails 2.3.2).� In rspec (1.2.6) I get an error (Missing template /comments/_comment.erb in view path It seems that rspec when running helper tests doesn't set the view_path so rails doens't know where to look for the templates.� Is there a way to run helper specs with the same setup as controller specs? There is no support for rendering in helper specs as of yet. Please file a feature request if you think there should be. I have, personally, never rendered from a helper. Anybody else? ___ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users
Re: [rspec-users] rspec2 not working with shoulda
On Wed, Aug 11, 2010 at 10:38 AM, David Chelimsky dchelim...@gmail.comwrote: Also - the subject line is backwards - it shoulda been shoulda not working with rspec-2 ;) It's been a long time since I tried shoulda, but I used to like to give the advice, you shoulda used RSpec instead. ;-) Hope you get everything working, Nadal. Regards, Craig ___ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users
Re: [rspec-users] rspec2 not working with shoulda
On Wed, Aug 11, 2010 at 12:53 PM, Nadal node.j...@gmail.com wrote: I posted a question on shoulda forum. It is under moderation so I don't have a link yet. To the people who do not use shoulda: How would you write a test for above case. I assume it would require set up a subject, set nil value, and then see if there is an error message. I still think that for such cases should macro is succint and does it job well unless I am missing some new feature of rspec2. I don't yet use RSpec 2, but I don't think you're missing anything. Shoulda may very well be a good fit for these kinds of examples. Since you asked, here's how I currently write examples like the ones you posted. require 'spec_helper' describe User, being valid do it requires an email do user = User.new(:email = nil) user.should_not be_valid user.should have(1).error_on(:email) end it requires a name do user = User.new(:name = nil) user.should_not be_valid user.should have(1).error_on(:name) end end Regards, Craig ___ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users
Re: [rspec-users] [rails] shoud I test validate_presentence_of series?
I like covering validation with RSpec instead of only relying on it being covered at a higher level. I like to check for error(s) on each attribute to be sure that the model is not invalid for some other reason. Here's how I do it: describe Forum, 'being valid' do it requires a name do forum = Forum.new(:title = nil) forum.should_not be_valid forum.should have_at_least(1).error_on(:title) end it requires a code do forum = Forum.new(:tag = nil) forum.should_not be_valid forum.should have_at_least(1).error_on(:tag) end it requires content do forum = Forum.new(:content = nil) forum.should_not be_valid forum.should have_at_least(1).error_on(:content) end end Note that, in this example, you can put all of your validates_presence_of declarations on one line. validates_presence_of :title, :tag, :content Regards, Craig ___ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users
Re: [rspec-users] [Rspec-Rails] before(:all) doesn't rollback
More info here http://rspec.info/documentation/before_and_after.html. Note the warning at the bottom of the page. Regards, Craig ___ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users
Re: [rspec-users] testing named_scope
On Mon, Jan 18, 2010 at 5:17 AM, David Chelimsky dchelim...@gmail.comwrote: On Mon, Jan 18, 2010 at 4:01 AM, Pat Maddox mailingli...@patmaddox.com wrote: ... class User ActiveRecord::Base named_scope :admins, :conditions = {:admin = true} end describe User, admins do it should include users with admin flag do admin = User.create! :admin = true User.admin.should include(admin) end it should not include users without admin flag do admin = User.create! :admin = false User.admin.should_not include(admin) end end Small style matter, but I've leaning towards more declarative sounding example names: describe User, .admins do it includes users with admin flag do admin = User.create! :admin = true User.admin.should include(admin) end it excludes users without admin flag do non_admin = User.create! :admin = false User.admin.should_not include(non_admin) end end class User ActiveRecord::Base named_scope :admins, :conditions = {:admin = true} end We still have 'should' in the examples, but this produces more 'spec-like' output: User.admins includes users with admin flag excludes users without admin flag I agree, David. I've been omitting should from the beginning. For me, it made the start of every example look too similar. Regards, Craig ___ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users
Re: [rspec-users] [rails] strange regex validation failure
Hi Andy, Nothing jumps out at me at the moment. If you think there might be a difference between dev and test, you could launch script/console with the test env. and try it again on the console. $ script/console test Regards, Craig -- Craig Demyanovich Mutually Human Software http://mutuallyhuman.com ___ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users
Re: [rspec-users] any eta on the rspec book?
Information on the book is always available here: http://pragprog.com/titles/achbd/the-rspec-book Currently, the ETA is April 2009. I'm sure that the authors will let us know if anything changes. Regards, Craig ___ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users
Re: [rspec-users] Specing protected methods
On Thu, Oct 23, 2008 at 7:49 AM, Bastien [EMAIL PROTECTED] wrote: I m already specing the generate_reports methods with a mock on sub_total, but I would also like to spec that method. I've put this method into protected because it doesn't make sens to call it from outside this class. But you're arguing that I should put that method into a lib ? I m not sure it would make sense as this method is very specific to that class, and would actually be easy to spec. Except that it's not possible to call a protected method from a spec as far as I know, correct me if I m wrong. (I maybe didn't explain my problem well in the first place) If you're somehow mocking the sub_total method while specing the generate_reports method, don't do that. Don't stub/mock the thing that you're specing, only stub/mock its collaborators, i.e., its dependencies. I would either verify via generate_reports that the sub totals on the report are correct, or I'd factor out something like a ReportGenerator class and spec it. Then, I'd stub/mock out the ReportGenerator in the specs for the Survey class. Regards, Craig ___ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users
Re: [rspec-users] How to spec accessing a constant
Cool. Having seen something a little more concrete, I like your design decisions. In this case, I'd go with Scott's recommendation of hiding the constant behind a method. Regards, Craig ___ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users
Re: [rspec-users] How to spec accessing a constant
Probably, I would just check the outcome of the method instead of checking interaction with a constant. Craig ___ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users
Re: [rspec-users] How to spec accessing a constant
On Wed, Oct 15, 2008 at 8:47 PM, Nick Hoffman [EMAIL PROTECTED] wrote: On 2008-10-15, at 16:39, Craig Demyanovich wrote: Probably, I would just check the outcome of the method instead of checking interaction with a constant. Craig So you guys wouldn't worry about the spec for class A being coupled to this constant in class B? Since class A is coupled to class B, the specs for A are also coupled to class B through class A. Thus, I wouldn't worry about the coupling. Why does a method of class A directly access a constant of class B? Does the constant belong in class A? Does the method belong in class B? If you can and want to be more specific with your code and specs, I'm sure that we can all write some specs together. Regards, Craig ___ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users
Re: [rspec-users] Problems mocking controller helpers with the last version.
I can't tell from your post whether you meant to type an instance or local variable when noting that you could check the role of the user in the view with current_user.role, since you mentioned an instance variable earlier. assigns[:current_user] = ... sets up an instance variable, @current_user, for use in your view. You should be using @current_user instead of current_user. Regards, Craig ___ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users
Re: [rspec-users] Not sure why this controller spec isn't working
I'm assuming RSpec 1.1.3+, but here's how I might write this example: describe NotesController do it renders 'notes/new' when an empty note is submitted do Note.stub!(:new).and_return(stub(note)) organization = stub_model(Organization, :notes = stub(notes, : = false)) assigns[:organization] = organization post :create, organization_id = organization.to_param, new_note = { body = } response.should render_template(notes/new) end end I'm favoring the use of stub_model and stubs instead of mock_model (and mocks) because we're not setting any expectations on the note or the organization. Rather, we're just setting them up to deliver values that are either simply required (as in Note.new returning something non-nil) or what we want (the addition of the note to the organization to fail). I've also eliminated the idea of having existing notes on the organization. It's not central to what's being spec'd, so it's out. Furthermore, I made the submitted note's body empty because that's what was in the original description of the it block. That those two didn't match bothered me. Finally, I simplified the description of the it block to what I thought the essence of your example was; I hope that I'm at least close. :-) Please let the list know if this helped or if you already revisited the problem and solved it. I think we're interested in what ends up working for you. Regards, Craig ___ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users
Re: [rspec-users] Cookies - how to set in controller specs?
My pair and I struggled with this a little bit last week. We decided to just use a hash to represent cookies in specs, since that's how we're interacting with it. You can see the specs that we wrote here: http://pastie.org/226443 . Regards, Craig ___ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users
Re: [rspec-users] Cookies - how to set in controller specs?
I'm thinking of replacing the existing cookies() method (in the example) with one that returns a CookieJarProxy that will allow you to do any of the following: cookies['cookie_key'] = 'cookie value' cookies[:cookie_key] = 'cookie value' cookies['cookie_key'].should == 'cookie value' cookies[:cookie_key].should == 'cookie value' WDYT about that? David, do you plan to allow checks that other values are set as we'd expect? Other values include: * :value * :path * :domain * :expires * :secure * :http_only http://www.railsbrain.com/api/rails-2.1.0/doc/index.html?a=M000352name=cookies For example, I'd like to verify that I update the value and expiration date: it resets the cookie with the value and new expiration date do get_index cookies[:project_sort][:value].should == name cookies[:project_sort][:expires].beginning_of_day.should == 1.year.from_now.beginning_of_day end Thanks, Craig ___ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users
Re: [rspec-users] Newbie: lambda do...end.should change(Model, :count).by(1). Doesn't work
A syntax note from the RSpec docs: blocks passed to should change and should_not change must use the {} form (do/end is not supported). http://rspec.info/rdoc/classes/Spec/Matchers.html#M000386 Regards, Craig ___ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users
Re: [rspec-users] [rspec-devel] rspec_on_rails (trunk - r3070) works with Rails 2.0.1
On Dec 8, 2007 11:47 PM, James Deville [EMAIL PROTECTED] wrote: I figured most of it out. The Spec::Rails stuff was something in the code which has been fixed by revision 3099. The test methods partially make sense. Since the test/unit code has been integrated, methods with test in them are automatically turned into specs. However, the test? method is in a lib file that isn't directly loaded into the specs. It is a convenience method: def test?; RAILS_ENV==test;end I don't know why this method is being included in my tests, every context has it as an example. Any ideas? I have a couple ideas. If the lib file is on the load path, then maybe rspec is searching it. Also, re: the name, maybe rspec and/or test::unit is recognizing the method as a test because it begins with test. Try renaming the method. Regards, Craig ___ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users
Re: [rspec-users] specifying an HTTP status code
On Dec 1, 2007 5:19 PM, Scott Taylor [EMAIL PROTECTED] wrote: On Dec 1, 2007, at 5:10 PM, Jarkko Laine wrote: On 1.12.2007, at 23.27, Scott Taylor wrote: it should return an HTTP status code of 404 do get :view response.headers[Status].should == 404 Not Found or response.response_code.should == 404 Nice. I find out something new every day from this list. You might also look at status_codes.rb. For example, if you have rails in vendor/rails, it's path is vendor/rails/actionpack/lib/action_controller/status_codes.rb For any status in that file, you should be able to do this in your examples: assert_response :not_found Yes, that's a Test::Unit assertion. Here's why I use it: * I'm not sure if there is a way to do it in RSpec (haven't yet investigated) * I can use Test::Unit assertions in RSpec (really cool) * I like the way it reads compared to using the codes, such as 404 Regards, Craig ___ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users