简体   繁体   English

NoMethodError运行集成测试,RailsTutorial Ch9

[英]NoMethodError running Integration Test, RailsTutorial Ch9

I'm having an issue where my integration tests do not seem to find the log_in_as method from my test_helper.rb 我遇到一个问题,我的集成测试似乎无法从我的test_helper.rb中找到log_in_as方法

I have been following Michael Hart's Rails tutorial, so I was hoping not to massively refactor my code to try and get this to work. 我一直在关注Michael Hart的Rails教程,所以我希望不要大规模重构我的代码以尝试使其正常工作。 I would like to continue on through the book without having to exclude the tests, since it is pretty test heavy afterall. 我想继续阅读本书,而不必排除测试,因为毕竟这是相当繁重的测试。

Error: 错误:

UsersLoginTest#test_login_with_remembering:
NoMethodError: undefined method `log_in_as' for #<UsersLoginTest:0x00000005b18460>
    test/integration/users_login_test.rb:43:in `block in <class:UsersLoginTest>'

User_login_test.rb: User_login_test.rb:

require 'test_helper.rb'

class UsersLoginTest < ActionDispatch::IntegrationTest

.
.
.

  test "login with remembering" do
    log_in_as(@user, remember_me: '1')
    assert_not_empty cookies['remember_token']
  end

  test "login without remembering" do
    # Log in to set the cookie.
    log_in_as(@user, remember_me: '1')
    # Log in again and verify that the cookie is deleted.
    log_in_as(@user, remember_me: '0')
    assert_empty cookies['remember_token']
  end
end

test_helper.rb: test_helper.rb:

ENV['RAILS_ENV'] ||= 'test'

class ActiveSupport::TestCase
  fixtures :all

  # Returns true if a test user is logged in.
  def is_logged_in?
    !session[:user_id].nil?
  end

  # Log in as a particular user.
  def log_in_as(user)
    session[:user_id] = user.id
  end
end

class ActionDispatch::IntegrationTest

  # Log in as a particular user.
  def log_in_as(user, password: 'password', remember_me: '1')
    post login_path, params: { session: { email: user.email,
                                          password: password,
                                          remember_me: remember_me } }
  end
end

I had this same issue. 我有同样的问题。 There are two problems I had to fix: 我必须解决两个问题:

  1. Make sure there is only one test_helper.rb file, and 确保只有一个test_helper.rb文件,并且
  2. test_helper.rb is in the right folder test_helper.rb在正确的文件夹中

Hope this helps! 希望这可以帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Rails 5集成测试失败,出现NoMethodError:使用Devise helper sign_in时nil:NilClass的未定义方法&#39;[] =&#39; - Rails 5 Integration test fails with NoMethodError: undefined method `[]=' for nil:NilClass when using Devise helper sign_in Hartl Ruby on Rails教程4章10,用户编辑测试失败 - Hartl Ruby on Rails tutorial 4 ch 10, users edit test fails users_signup_test.rb 在断言 users.reload.activated 时失败。 期望假为真。 Railstutorial Hartl - users_signup_test.rb fails at assert users.reload.activated. Expected false to be truthy. Railstutorial Hartl NoMethodError:nil:NilClass的未定义方法“ body”(测试失败) - NoMethodError: undefined method `body' for nil:NilClass (test failure) NoMethodError:controller 测试中未定义的方法“assert_emails” - NoMethodError: undefined method `assert_emails' in controller test Rails 集成测试中的多个用户 - Multiple users in a rails integration test 集成测试会话未加载会话Cookie - Integration Test Session not loading Session Cookie 为集成测试替换 rails 控制器测试应该始终坚持到 db? - Replacing rails controller tests for integration test should always persist to db? Rails 5集成测试将Fixture_file_upload转换为字符串 - Rails 5 integration test converts fixture_file_upload to string (第5条)该集成测试关键字params参数为什么引起弃用警告? - (Rails 5) Why is this integration test keyword params argument causing a deprecation warning?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM