blog.sowatchasayin

Rails etc.

shouldaでlogin_requiedのテスト

shouldaです。
ログイン用のマクロを作った。

こういう書き方ができるのはいいすね。これはauthlogicですが、authlogic以外でも勿論できる。

context "ArticlesController" do
should_require_login :get, :new
should_require_login :post, :create
should_require_login :get, :edit
should_require_login :put, :update
should_require_login :delete, :destroy
end


でも、リダイレクト先でテストは適当すぎる…
class Test::Unit::TestCase
def self.should_require_login(http_method, method)
should "require login wth #{http_method.to_s.upcase} :#{method}" do
raise "http verb(#{http_method} is not my cup of tea)" unless [:put, :get, :post, :delete].include? http_method.to_sym
if UserSession.find
UserSession.find.destroy
end
eval(http_method.to_s + "(\"#{method}\")")
assert_redirected_to new_user_session_path
end
end
end


余談ですが、ついにauthlogicがrestful_authenticationを追い越した。

No comments:

Post a Comment

Author

Fujimura Daisuke
http://fujimuradaisuke.com

Labels