Who are we?

We are a group of enthousiastic people working for Nedap, a Dutch company with a single goal: solve problems.

Search
« TNT Ruby Webservice | Main | Cha-Ching and dutch banks »
Wednesday
Jan232008

Testing your application controller with Rspec

I was trying to create a function that would check the enforcement of the before filters in my application controller. After going through a lot of rspec documentation and examples, I found nothing that really suited my needs. After a long google, I found a mention in the rspec mailing list and a hint to a solution for this. This is a working example of this idea.

I have in my application controller the following code:

class ApplicationController < ActionController::Base
  before_filter :check_authorization
   ....
  # Checks if a user is authorized
  def check_authorization
    User.check_authorization(controller_name, action_name)
  end
end

And in spec/controllers/application/application_spec.rb the following description.

describe "an authorized controller", :shared => true do

  it "should have the check_authorization set in the before filter" do
    ApplicationController.before_filters.should \
      include(:check_authorization)
  end   
end

In all other controller specs I can now do this:

require 'application_controller_spec'

describe UnitsController, "in general" do
  it_should_behave_like "an authorized controller"
end

By doing this I now have a spec that ensures that every controller checks authorizations before doing anything else. How cool is that :)!

It would be nicer if the test handled skip before_filters in some nice way like:

it_should_behave_like "a monkey".except_for_action('show')
Has anyone got any ideas for that?

Special thanks to Matthijs Langenberg for his insights.

PrintView Printer Friendly Version

EmailEmail Article to Friend

References (1)

References allow you to track sources for this article, as well as articles that were written in response to this article.
  • Response
    Response: b2b marketing
    Superb Web page, Stick to the wonderful job. Thanks a lot.

Reader Comments (8)

This will accumulate your affluence Bell Ross in the best action possible, and will aswell ensure that they angle the analysis of time!Your Bell Ross Watches ability abiding yachtmaster dejected punch 116689 Replica Bell Ross will endure abundant longer, and will be in abundant bigger condition, if you analyze it to your car. You wouldn't let your Replica Bell Ross Watches get damaged and leave it, or leave it afterwards application it regualry and accepting the MOT. 马文鑫测试

September 21, 2010 | Unregistered Commenterreplica bags

In this way, instead of looking odd or wearied, ugg boots seem to be quite "in". You may be the attractive part in the streets!In a word, the change from "ugly" ugg to an icon piece of fashion is determined by a lot of factors. No one can really tell fashion clearly but what we know is the fact that the uggs are really hot now! ugg infant's erin boots originally came from Australia. These sheepskin ugg infant's erin are loved by most people as they keep feet warm in freezing climate.

September 24, 2010 | Unregistered Commenterswf

This is a good article,thank you for your share,I like it very much.Our products are superior in quality and moderate in price and are sure to be saleable in your market,by virtue of this superior quality, this product is often sold out in many areas. This new product is really much better than the previous one. You have to try it to believe it.fendi handbags

November 22, 2010 | Unregistered CommenterDelores R. Garcia

louis vuitton borseIn the process of service for the queen, the ups and downs of travellers, Louis vuitton has caused the attention. At the time of the revolution is in the ascendant, traffic tools on the train become the most fashionable choice, travelers, however it also brought them to a lot of trouble: not the clothes made crumpled advisory suitcase on the train, is the turbulence in bags and fall down. Louis vuitton think for more people from traveling to the sorrow, ended in 1854 for the palace of the service work in Paris, started the first leather store, the main products between is flat cover suitcase. louis vuitton online shopFounder Louis vuitton's first profession is noble when you travel for celebrities pack up. He witnessed the invention of the steam trains, also saw the steamboat transportation development, at the same time also deeply realize at the time of mammoth dome of the suitcase difficulties. So, Louis vuitton revolutionary created a roof to leather box in Paris, and opened the first shop.gucci online shopThe Trianongrey "with" made of canvas boxes soon become Paris high society like travel the nobles of the first choice of travel items. His design soon be copied, flat-roofed square suitcase become a fashion. Four years later, Louis vuitton expanded leather store scale, near Paris Asnieres set up the first plants. This time, the design, the production process of paying more attention to the solution of practical problems travellers, with practical design idea as foundation, Louis vuitton in fashion and professional aspects further.Louis Vuitton 2011 # Louis Vuitton Bags # Louis Vuitton Damier Handbags # Louis Vuitton Damier Purses # Louis Vuitton Damier Wallets # Louis Vuitton Sunglasses # Louis Vuitton Luggage # Louis Vuitton Monogram Handbags # Louis Vuitton Monogram Purses # Louis Vuitton Monogram Wallets # Louis Vuitton Purses 2011 # Louis Vuitton Scarves # Louis Vuitton Shoes # Antheia Handbags # Cosmic Blossom Handbags # Damier Azur Canvas Handbags # Damier Ebene Canvas Handbags # Damier Ebene Canvas Bags # Damier Graphite Canvas Bags # Damier Purses # Damier Wallets # Epi Leather Handbags # Fashion Bags # Gucci Sunglasses # Gucci Handbags # Gucci Luggage # Gucci Man Bags # Gucci Purses # Gucci Shoes # Gucci Wallets # Gucci Women Bags louis vuitton outlet louis vuitton outlet

June 24, 2011 | Unregistered Commenterlouis vuitton borse

UGG Bailey Button Boots 5803 Bridal shoes come in many different styles, colors and designs. New Adidas Shoes You could go for regular pumps or you

November 1, 2011 | Unregistered CommenterCheap Asics Shoes

top quality replica wrist watches tend to be my personal mug associated with teas. I've loved replica panerai radiomir after i joinedreplica vacheron constantin, replica iwc pilot watches) tend to be the most popular wrist watches, You will find a myriad of (replica rolex watches sale with this online shop, if you want this, make sure you go to this particular shop, you'll have a ball!!!

November 18, 2011 | Unregistered CommenterAnthony123

se a ugg boots

you cheap uggs can use a ugg boots reliable third ugg outlet party PST Splitter tool They are made of high quality materials uggs that www.cheapuggscomeon.com
Related:
1 2 3 4 5 6 7 8 9 10

December 2, 2011 | Unregistered Commentersunnyfel

Ik weet niet eens hoe ik stopte up hier, maar ik nam dit indienen goed worden gebruikt. Ik beseffen niet wie je bent maar zeker je gaat naar een bekende blogger voor degenen die niet al Proost! jurken online

October 2, 2013 | Unregistered CommenterEdward Burns

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>