[英]Ruby on Rails: How can I sign in with devise inside of my tests?
I'm having issues signing into Devise in my tests. 我在测试中登录Devise时遇到问题。 I'm using the default testing suite in Rails.
我在Rails中使用默认的测试套件。
In my test helper file, I've defined a log_in method like so: 在我的测试帮助文件中,我定义了一个log_in方法,如下所示:
def log_in
@user = User.create(id: 1)
sign_in(:user, @user)
end
I don't get any errors, the method just doesn't sign me in. I know this because I redirect the user when they are not signed in, and the result of one of my tests is: 我没有任何错误,该方法只是没有登录我。我知道这是因为我在用户未登录时重定向了用户,而我的测试之一的结果是:
ContentsControllerTest#test_should_show_content [E:/Uni/coursework/empire/test/controllers/contents_controller_test.rb:32]:
Expected response to be a <success>, but was <302>
sign_in is called in setup like so... 像这样在安装程序中调用sign_in ...
setup do
@content = contents(:one)
log_in
end
Any help on how I can sign in would be most appreciated. 对于如何登录的任何帮助将不胜感激。
You also should 你也应该
include Devise::TestHelpers
Here you have some code samples Minitest+Devise 在这里,您有一些代码示例Minitest + Devise
I've fixed my issues by changing my log_in method to the following: 我通过将log_in方法更改为以下内容解决了问题:
def log_in
sign_in(users(:one))
end
The reason why I wasn't using a fixture in the first place was because I had an issue with the fixture which was causing some strange errors. 首先我不使用固定装置的原因是因为我遇到固定装置问题,导致了一些奇怪的错误。 Always check your fixtures.
务必检查您的灯具。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.