简体   繁体   English

Ruby on Rails:如何在测试内部使用devise登录?

[英]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.

相关问题 如何在使用Devise(Ruby On Rails)注册时自动创建地址参数 - How can I automatically create an adress parameter upon sign up with Devise (Ruby On Rails) 如何使用Devise in Rails通过api登录? - How can I sign in via api using Devise in Rails? 如何从 Rails 控制台注销设计用户? - How can I sign out a devise user from the Rails console? 将Devise与Ruby on Rails一起使用,如果用户使用无效的auth_token,如何将用户发送到sign_in路径? - Using Devise with Ruby on Rails, how can I send the user to sign_in path if they use an invalid auth_token? 使用Devise / Rails 4 / Minitest运行控制器测试时无法登录 - Can't sign in when running controller tests with Devise / Rails 4 / Minitest Ruby on Rails:Devise + Cancan-如何编辑其他用户? - Ruby on Rails: Devise + Cancan - How can I edit other users? 如何在Devise(Ruby on Rails)中阻止url地址 - How can I block url adress in Devise (Ruby on Rails) Ruby on Rails 如何添加功能来设计的注册创建操作? - Ruby on Rails how can add functionality to devise's sign up create action? 如何在“登录”和“注册”页面上使用Rails Devise设置seo标题标签? - How can I set seo title tags with Rails Devise on the Sign In and Sign Up pages? Ruby on Rails 3-如何基于Devise创建2种不同的表单进行注册 - Ruby on Rails 3 - How to create 2 different forms for sign up based on Devise
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM