使用Capybara测试令牌身份验证的正确方法是什么?这是我所拥有的:
describe ApplicationController do
let!(:user) { create :user, authentication_token: 1 }
specify "token should work now" do
visit "/?auth_token=#{user.authentication_token}"
# get :index
response.should_not redirect_to(new_user_session_url)
end
specify "token shouldn't work in the future" do
Timecop.travel(2.weeks)
visit "/?auth_token=#{user.authentication_token}"
# get :index
response.should redirect_to(new_user_session_url)
Timecop.return
end
end
如果我没有get :index
行,测试将失败。但如果我注释掉那行,测试就被保存了,但这不应该是正确的方式,因为我在上面有visit
我做错了什么?
事实证明visit
并没有将变量传递给控制器。将其更改为get :index, auth_token: user.authentication_token
解决了此问题。