Using Shoulda to test login – should_require_login
I've had my problems with shoulda, but one very powerful component of the testing framework is the ability to create macros. Here's one that checks to make sure a user has to be logged in to access an action. Put it into test/shoulda_macros/authentication.rb. (You can name the file anything you want I just thought authentication.rb made sense)
Test::Unit::TestCase.class_eval do def self.should_require_login(*actions) actions.each do |action| should "Require login for '#{action}' action" do get(action) assert_redirected_to(login_url) end end end end </ruby> Then inside your controller test do something like this: <pre lang="ruby"> class UserControllerTest < ActionController::TestCase should_require_login :edit, :update, :destroy end
-
http://www.alexjsharp.com Alex Sharp
-
http://rubenonrails.com Ruben
-
http://www.justinball.com Justin Ball
-
http://rubenonrails.com Ruben
-
http://www.rubyinside.com/shoulda-roundup-elegant-maintainable-ruby-testing-1723.html Shoulda Roundup: Elegant, Maintainable Ruby Testing
-
http://www.rubyinside.com.br/coletanea-de-artigos-sobre-o-shoulda-1375 Coletânea de artigos sobre o Shoulda


Justin Ball is the CTO for OERGlue.com a new startup trying to make it easy to mashup the web to create authentic learning experiences. He's been a software consultant and entrepreneur in the education space for more than 10 years. In the rare moments when he isn't writing code, talking about code or measuring his code productivity in profanity per hour, you can find him on his bike in the mountains or on the roads surrounding Cache Valley. 









