Rails on the Run

Rails experiments by Matt Aimonetti

Browsing Posts tagged RSpec

resftul_authentication is a great plugin, but not always resftful… especially if like me you dropped unit test for RSpec.

I have a set of RSpec examples I use all the time on all my projects requiring restful_authentication, obviously I always end up tweaking them because each application has its specific needs.

However, I was recently asked what’s the way of dealing with controllers specs when a controller has a limited access ?

Let’s look at the following controller for instance:

class AssetController < ApplicationController
 before_filter :login_required, :except => [ :index, :show ]

my assets_controller_spec.rb file will have the following code:

describe AssetController, "handling GET /assets/new" do

  before do
    @asset = mock_model(Asset)
    Asset.stub!(:new).and_return(@asset)
  end

  def do_get
    get :new
  end

  it "should be successful" do
    do_get
    response.should be_success
  end

end

and guess what… it will fail! Why? simply because we are trying to access an action requiring login. Right, so what’s the best way of dealing with this issue and get our test to pass?

The best solution I found was to use a simple helper that I put in my spec_helper.rb file

def login_as(user)
  case user
    when :admin
      @current_user = mock_model(User)
      User.should_receive(:find_by_id).any_number_of_times.and_return(@current_user)
      request.session[:user] = @current_user
    else
      request.session[:user] = nil
  end
end

You might wonder why I use when :user, well, in a lot of the application I’m working on, I have different levels of access and I want to tests different accounts so I the case conditional loop.

Anyway, let’s implement our helper in our previous RSpec example:

describe AssetController, "handling GET /assets/new" do

  before do
    login_as :admin
    @asset = mock_model(Asset)
    Asset.stub!(:new).and_return(@asset)
  end

  def do_get
    get :new
  end

  it "should be successful" do
    do_get
    response.should be_success
  end

end

and there you go, now your example passes properly ;)

Have fun

Recently I helped few people moving to Rails Edge and start using RSpec. I realized that I learned few tricks and even if for me everything seemed quite simple, things are not that simple when you recently started with Rails.

This would work on Mac and Linux, sorry Windows users, you’ll have to slightly change the code below.

Live on the Edge

This is actually a bit tricky but it was very well covered by John Nunemaker in this post

To sum John’s post:

Create a normal rails project:

$ rails my_project
$ cd my_project

$ rake rails:freeze:edge

(or use checkout edge in the vendor folder)

$ cd ..
$ ruby my_project/vendor/rails/railties/bin/rails my_edgie_project

You now have have a real Edge project called myedgieproject. You can delete the my_project folder since we only used it to create our real edge project.

Now, we are not really done since we need to add the Edge files into our vendor folder so we don’t use our local rails gem.

I would refer to another post from John, that you can find there

$ mkdir ~/rails
$ cd ~/rails
$ svn co http://dev.rubyonrails.com/svn/rails/trunk .

We just created a rails folder called rails in our home folder and we checked out edge/trunk in it.
Now let’s go in our Rails app and setup a symlink to the trunk folder we just created.

$ cd ~/rails_projects/my_edgie_project
$ ln -s ~/rails/trunk vendor/rails

If you are using subversion, you can ignore the symlink so it doesn’t try to version it:

$ svn propset svn:ignore "rails" vendor/
$ svn commit -m "using new sweet rails setup as recommended by John Nunemaker"
$ svn up

Read more for advanced settings etc…

Install RSpec

Very straight forward, you just need to follow the documentation

$ cd ~/rails_projects/my_edgie_project
$ ruby script/plugin install svn://rubyforge.org/var/svn/rspec/tags/CURRENT/rspec
$ ruby script/plugin install svn://rubyforge.org/var/svn/rspec/tags/CURRENT/rspec_on_rails

If you have TextMate, you might want to download the latest RSpec-X.Y.Z.tmbundle.tgz Bundle

Next thing you want to do is to install ZenTest

$ sudo gem install ZenTest

Make sure you install all the required packages.

If you are using Growl create a new file called .autotest in your home directory:

$mate ~./autotest

and add the following 2 lines to be warned when your specs/examples fail/pass

require 'autotest/redgreen'
require 'autotest/growl'

Now, lets go back to our project and create a model using the rspec scaffold (it uses scaffold_resource generator and create all the specs for you)

$ cd ~/rails_projects/my_edgie_project
$ ruby script/generate rspec_scaffold User first_name:string last_name:string :age:integer

Now, let’s start autotest (from zentest) so out code is tested in the background

$ autotest

There you go, really to modify your RSpec examples, make them fail, fix your code, examples should pass, refactor your code and start again :)

DrNic the most famous Australian Rails developer surprisingly doesn’t spend most of his time working on Rails read interview
That’s maybe why he recently became so active in the Rails community ( see Magic Multi-Connections, Magic Models, map_by_methods, Gem Generator etc..)

Today he released something very helpful for the Rails Community, not another Gem or another cool plugin to extend Rails but a web application to help you planning your conferences. We already had other tools such as conference meetup but it’s the first time that we get a product helping you to plan a conference by scheduling the sessions you want to attend.

screenshot

screenshot of the entire schedule

I had a quick chat with DrNic about his latest creation and here is what he said:

“its been fun building just how I thought session selection might look + feel
web2.0 = permissive voyeurism I think!”

I share the same vision than Nic on session selection and I really enjoyed booking my Rails sessions and seeing what other people selected. (I guess that’s my voyeur side, don’t you like web2.0?)

If you wanna see what I planned on attending, checkout my schedule: my schedule

Thanks Nic for the good work and too bad you are not presenting anything at the RailsConf (move your bum to come up with a better submission next year!)