是否可以在集成测试中设置 cookie 何时过期?

Is it possible to set when a cookie expires inside an integration test?

在集成测试中是否有一种方法可以操纵 cookie 的到期日期(以测试它是否被正确考虑)。例如,如果 cookies['user_id'] 已经存在于测试中,则像这样操作它:

cookies['user_id'].expires = Time.zone.now - 1.day

(这​​个returnsundefined method 'expires=')
我在 Rails.

中使用 MiniTest

您可以在测试中使用TimeCop时间旅行:

def setup
  do_something_which_sets_cookie
  Timecop.freeze(1.month.from_now) 
  # or how ever long it takes the cookie to expire
end

def teardown 
  Timecop.return
end

如果您要测试的是您的应用程序在一段时间后的行为而不将测试与实现细节联系起来,这可能是一个不错的方法。

否则你只需要在设置过期之前确保 cookie 存在:

cookies['user_id'].expires = Time.zone.now - 1.day if cookies['user_id']