【发布时间】:2011-08-16 18:19:58
【问题描述】:
我正在使用签名的 cookie 来跨页面维护我的用户,几乎实现为 here。
我使用两种方法,sign_in(account) 和 sign_out 来操作我的 cookie,按照您的预期创建或销毁它们...
module SessionsHelper
def sign_in account
cookies.signed[:auth_account] = {
:expires => 1.week.from_now,
:value => [account.id, account.hash]
}
end
def sign_out
cookies.delete(:auth_account)
end
end
但是,当尝试使用此方法或功能测试中 ApplicationController 中与之匹配的身份验证方法时,我收到 NoMethodError:
NoMethodError: {}:ActiveSupport::HashWithIndifferentAccess 的未定义方法“签名”
我从this 和this 了解到,这是在测试用例中以不同方式定义cookie 的问题,但我无法让这些解决方案中的任何一个起作用。为了完整起见,一个因上述错误而失败的示例测试:
require 'test_helper'
class AccountsControllerTest < ActionController::TestCase
include SessionsHelper
setup do
# We need to fake authentication by manually
# assigning an account to the sign_in process
sign_in accounts(:ia)
@account = accounts(:ia)
end
test "should 403 on index if unauthenticated" do
sign_out
get :index
assert_response :forbidden
end
test "should 200 on index if authenticated" do
get :index
assert_response :success
end
end
【问题讨论】:
-
另外,作为旁注,使用 cookies.permanent 时也会出现此错误。我通过手动设置过期时间来解决这个问题,但这是同样的问题。
标签: unit-testing cookies ruby-on-rails-3.1