除非是管理员,否则不允许任何人查看用户页面。因此,如果用户没有登录,他们将无法离开主页。在 user_controller_spec 中,我们有几个关于编辑个人信息或密码的测试。但是测试永远不会通过,因为我们在规范类中创建的用户没有登录。有没有办法在 user_controller_spec 中登录用户?
最佳答案
如果您使用 Devise 进行身份验证,它会提供辅助方法:sign_in
所以你可以这样做:
it "should do something" do
sign_in(user) # user is an object of your User model
#Continue testing
end
参见 this documentation获取更多信息。
关于ruby - 如何在 user_controller_spec 中以用户身份登录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13244925/