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)

1.   

2.  Test::Unit::TestCase.class_eval do

3.    def self.should_require_login(*actions)

4.      actions.each do |action|

5.        should "Require login for '#{action}' action" do

6.          get(action)

7.          assert_redirected_to(login_url)

8.        end

9.      end

10.   end

11. end

12. 

13.  

14. Then inside your controller test do something like this:

15.  

16. 

17. class UserControllerTest < ActionController::TestCase

18.   should_require_login :edit, :update, :destroy 

19. end

20. &nbsp;

Comments

blog comments powered by Disqus