我想用这个测试测试登录/注销方法:
测试/控制器/sessions_controller_test.rb
class SessionsControllerTest < ActionController::TestCase
fixtures :users
include SessionsHelper
test "should signin and signout" do
get :new
assert_response :success
post :create, email: users(:alex).email, password: 'qwerty'
assert_redirected_to profile_url
assert cookies[:remember_token]
assert current_user == users(:alex), 'Ooops! Wrong current_user!'
delete :destroy
assert_redirected_to signin_url
debugger
assert !cookies[:remember_token]
=> assert !current_user, 'Ooops! Current_user not nil!'
end
end
在=>点cookies[:remember_token]
消失了,但current_user
仍然存在users(:alex)
,我不明白为什么。我错过了delete :destroy
排队吗?
集成测试正在通过,并且在带有注销行为的浏览器中的实时性能也可以解决问题。
来自会话控制器和助手的方法:
session_controller.rb
def destroy
sign_out
redirect_to signin_url
end
session_helper.rb
def current_user=(user)
@current_user = user
end
def current_user
remember_token = User.encrypt(cookies[:remember_token])
@current_user ||= User.find_by(remember_token: remember_token)
end
def sign_out
cookies.delete(:remember_token)
self.current_user = nil
end