RSpec on Rails : RESTful Authentication

For a new Rail project I’m working on, I decided to try to use RSpec.

The main problem I see with this solution is that not so many plugins out there have RSpec tests and so it can be boring to have to run autotest and rspec_autotest in parralel to be sure both RSpec and Test::Unit tests passes.

Helped by a recent post of Yurii Rashkovskii : RSpec on Rails: acts_as_authenticated , I adapted and add a few specifications to be compatible with restful_authentication plugin.

Here are the files :

Just put those in spec/{models,controllers}, copy users.yml fixtures from test/fixtures to spec/fixtures/ and you’re done.

Note I did not spec the UserObserver for now.

[Update]

I refactored a little bit my previous files, add some specs and try to spec user_notifier and user_observer models as well.

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.

I’m waiting for your comments on notifier/observer specs, I didn’t see any examples of them in rspec and I’m not sure of the best way to spec them. For example, UserNotifier is an ActionMailer and resides in models directory, but it acts more like a controller (render views), well you see the point.

[Update]

For those of you who switch to RSpec 1.0.x, Jonathan Linowes has updated my files to make them compatible with the new syntax (thanks). You can grab them at : Rspec 1.0 and Restful Authentication

Tue, 02 Jan 2007 14:27 Posted in

  1. By Yurii Rashkovskii 26/01/2007 at 08h06


    Jonathan,

    it seems that if rails app is configured to use UTC,

    you need to change


    users(:quentin).update_attribute :remember_token_expires_at, 15.minutes.ago </pre>
    to

    users(:quentin).update_attribute :remember_token_expires_at, 15.minutes.ago.utc

  2. By Tron Jonathan 26/01/2007 at 10h55


    Yurii,
    thanks for you comments, I noticed that when I switched to UTC.

  3. By Jason 27/01/2007 at 01h47


    Hi
    New to RSpec. Have a clean implementation of restful_authentication. Cannot get “should_not_be_activated” to pass.
    What method is this testing? The activation_key and activated_at attributes are both nil in the test output. Bit confused.

  4. By Jonathan Tron 27/01/2007 at 09h55


    Hi Jason, should_not_be_activated is testing User#activated? which is implemented as :

    def activated? !! activation_code.nil? end

    In RSpec the should_be_methodname / should_not_be_methodname check if methodname? return true respectively false.

  5. By Jason 29/01/2007 at 11h24


    Hi Thx for reply.
    Here is my failed test:

    
    1)
    'A new User should not be activated' FAILED
    #<User:0x481b290 @password_confirmation="quire", @attributes={"activation_key_ex
    pires_at"=>nil, "activation_key"=>nil, "last_login_at"=>nil, "salt"=>nil, "activ
    ated_at"=>nil, "updated_at"=>nil, "crypted_password"=>nil, "remember_token_expir
    es_at"=>nil, "website"=>nil, "remember_token"=>nil, "display_name"=>"quire", "lo
    gin"=>"quire", "created_at"=>nil, "email"=>"quire@example.com"}, @errors=#<Activ
    eRecord::Errors:0x481a4d0 @errors={"login"=>["has already been taken"], "email"=
    >["has already been taken"]}, @base=#<User:0x481b290 ...>>, @password="quire", @
    new_record=true> should not be activated nil
    ./spec/models/user_spec.rb:44:
    
    

    Note I’ve changed to activation_key, anyway, maybe this makes sense and someone can suggest what might be wrong.

    Also, what is a !! in Ruby? I’ve searched through many online docs and cannot find reference to this.

    Much thx

  6. By Tron Jonathan 29/01/2007 at 12h18


    Jason,
    it seems your user is not created because of duplicate user login in database, hence the before_create callback is not called in User model and the activation code is not generated and stored in activation_key.

    About the !!, it’s not a particular syntax. I don’t really understand the need to double-negate a statement… so I assume the author has his reason :)

  7. By Jason 31/01/2007 at 13h56


    I’ve asked on the Newcastle ruby mail list and considered opinion is that double-bang is a double negative (try it out in irb), and therefore probably unnecessary.

    I also think checking activation using activation_key attribute is wrong and activated_at attribute should be used.

    Still can’t get tests to pass on this method (spec can’t find the method), but I’ve found that adding


    teardown do end

    has helped fix the “already been taken” errors.

  8. By Scott Brown 13/02/2007 at 21h04


    This sounds great, but I’m getting a 404 on all of the download links.

    Whence the files?

  9. By Jonathan Tron 13/02/2007 at 21h24


    Scott, sorry I forgot to copy files when I upgraded to typo trunk. It should work now.

  10. By Edd Dumbill 25/05/2007 at 14h45


    Did you make any progress with autotest? I want to support rspec and test/unit in parallel with autotest and can’t find any easy way of doing this.

  11. By Jonathan Tron 28/05/2007 at 20h22


    Edd : I didn’t tried to run both specs/tests at the same time with only one autotest, but if I recall correctly there were some guys trying to achieve this on RSpec mailing-list during last week.

  12. By linoj 30/05/2007 at 04h53


    hi, your files are not compatible with the Rspec 1.0 API. I’ve updated them so it works and posted it on my site
    http://www.vaporbase.com/postings/Rspec_1.0_and_Restful_Authentication
    Feel free to grab them and post them here to replace yours if you’d like.

  13. By Hannes 05/10/2007 at 18h05


    Double-bang is actually useful when you want to know if you have nil, false or objects.

    Consider:

    
    !1 # => false
    !!1 # => true
    
    !"a" # => false
    !!"a" # => true
    
    !nil # => true
    !!nil # => false
    
    !false # => true
    !!false # => false
    
    

    This means that double-bang can be read as is_not_falsy since nil and false are the only falsy values in Ruby.

    Left as excercise:

    
    !!?! # => true :)
    
    
  14. By Jonathan Tron 05/10/2007 at 20h58


    Hanes : Yes I read about the double-bang some times after posting my last comment. Thanks for the reminder !

  15. By David Spurr 18/07/2008 at 16h38


    How about testing that your observer is registered with Rails, is that possible?

  16. By Jonathan Tron 27/07/2008 at 21h23


    David : I thinks one step is to check your observer is observing the right class first, this could be done with : ActiveRecord::Observer#observed_class class method.
    To test the other way around, the ActiveRecord::observers class methods (mixed-in from ActiveRecord::Observing) should do it.

Comment RSpec on Rails : RESTful Authentication


RSS