lost password?

home
•  reviewramp
•  rails +
•  javascript
•  webdev
•  django
•  xaraya
•  xamp
•  musings

rss
Tag this page
   

ยป Blogs that link here
last modified: May 29, 2007
(first posted: May 29, 2007)
(13219 Reads)
keywords: restful authentication rspec
Permalink

Rspec 1.0 and Restful Authentication

Here are specs for the restful_authentication plugin that work with Rspec on Rails 1.03

I've started using RSpec on Rails for spec'ing (aka testing). More about that in a separate post (I haven't had time to write up a walkthrough of my great experience with it).

Anyway, my project uses restful_authentication plugin so I started looking around and found a set of spec files for it on Jonathan.inspect blog but they're out of date with regard to the the Rspec 1.03 API.

I've updated the files. I didnt make any changes to the code except what was needed to make it work with the current API. (In fact, for example, I did not change "context" to "describe" or "specify" to "it"). Here they are:

Download ra spec.zip

The zip includes the files

  • user_spec.rb : User model specs
  • user_notifier_spec.rb : UserNotifier model specs
  • user_observer_spec.rb : UserObserver model specs
  • users_controller_spec.rb : Users controller specs
  • sessions_controller_spec.rb : Sessions controller specs

Per Jonathan,

You should also add :

def set_mailer_in_test
ActionMailer::Base.delivery_method = :test
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.deliveries = []
end

to Spec::Rails::EvalContext class in your spec_helper.rb.

 

 

Rspec 1.0 and Restful Authentication

Posted by: David Chelimsky on May 29, 2007 10:57 PM
I'd recommend not monkey patching Spec::Rails::EvalContext because that class name is subject to change. Instead, use the config API for making helper methods available:
module Helpers #can be named anything
  def set_mailer_in_test
    ActionMailer::Base.delivery_method = :test
    ActionMailer::Base.perform_deliveries = true
    ActionMailer::Base.deliveries = []
  end  
end

Spec::Runner.configure do |config|
  config.include Helpers
end

#

Rspec 1.0 and Restful Authentication

Posted by: Jonathan Tron on May 30, 2007 02:07 AM
Hey, thanks for updating these files, they came from my early days working with RSpec, so they are not the most elegants specs out there. David, thanks for the new syntax. As I stated before these are old specs written as of RSpec 0.7.x/0.8.x hence the need for this monkey patching of Spec::Rails::EvalContext. If I recall correctly there were no other way of doing that at that time.

#

Rspec 1.0 and Restful Authentication

Posted by: court3nay on July 09, 2007 06:13 PM
I have a slightly different way of doing this. See http://sample.caboo.se/empty_rails_app/trunk/ I basically include the auth stuff as a module.

#

Rspec 1.0 and Restful Authentication

Posted by: Andy Pearson on October 20, 2007 02:51 PM
I am using these specs and after some tweaking they all examples seem to be passing. Except one! The Sessions controller should login with cookie - just refuses to work, if I change any of the specs, then it breaks more of them. I have read through the code several times and I can't see any reason for it failing. Please let me know if you have any idea how I can fix this. You can mail me at: hey [at] criticalwebdesign.co.uk Thanks in advance!

#

Rspec 1.0 and Restful Authentication

Posted by: linoj on October 20, 2007 03:20 PM
Hi Andy, they all are working for me. I'd suggest checking your test.log file, and/or debugging with ruby-debug (I've gotten to the point of putting 'debugging' statements almost any time something doesnt work mysteriously).

#

Rspec 1.0 and Restful Authentication

Posted by: Levi on October 24, 2007 11:33 PM
I'm not sure, but shouldnt UserNotifier.new really be UserMailer.new? I had about 45 errors, changed that then it fixed everything. I haven't ever used the activation code in RA, but decided to this time...and Im not that familiar with action mailer either...

#

Rspec 1.0 and Restful Authentication

Posted by: Harm on December 11, 2007 06:24 AM
@Andy, not sure if you figured it out yet but I had a similar error. This was caused by the fact that I move all the protected methods in the sessions controller spec to the spec_helper.rb. Doing so rendered the cookie_for method unable to create a cookie as that method depends on the presence of the users fixture. Now I pass the object User to the method instead of the symbol. Now it works. Hope that helps!

#

Rspec 1.0 and Restful Authentication

Posted by: Harm on December 11, 2007 07:56 AM
Not only posted I twice erroneously, my comment was off the mark as well. @Andy, sorry I was wrong. My error stemmed from the fact that the sessions controller action new place no call to the current_user method in AuthenticatedSystem. Precisely this call makes sure the user is logged in through a cookie (via the login_from_cookie method). If you do not call current_user your tests will fail.

#

Rspec 1.0 and Restful Authentication

Posted by: Tadd on May 03, 2008 09:10 PM
I'm just learning rspec and despite the now old syntax these examples were extremely helpful. Big thanks!

#

Post a new comment

How many days in a week?

Name :