【问题标题】:How do I write a helper for an integration test in Rails?如何在 Rails 中为集成测试编写帮助程序?
【发布时间】:2014-01-13 16:59:31
【问题描述】:

如何编写用于多个集成测试的集成测试助手?我已经尝试了以下错误。我正在考虑创建一个基类并对其进行扩展,但我不明白“test_helper”是如何工作的!我不能将辅助方法放在 test_helper 中,因为它们使用特殊的集成辅助方法,例如 post_with_redirect

LS

$ ls test/integration
integration_helper_test.rb  post_integration_test.rb  user_flows_test.rb

代码,integration_helper_test.rb

class IntegrationHelperTest < ActionDispatch::IntegrationTest

  def login(user)
    ...

代码,post_integration_test.rb

require 'test_helper'
require 'integration_helper_test'
# require 'integration/integration_helper_test'

class PostIntegrationTest < ActionDispatch::IntegrationTest
  # include IntegrationHelperTest

错误

$ rake
rake aborted!
cannot load such file -- integration_helper_test
C:/Users/Chloe/workspace/SeenIt/test/integration/post_integration_test.rb:2:in `<top (required)>'
Tasks: TOP => test:run => test:integration

代码,post_integration_test.rb

require 'test_helper'
# require 'integration_helper_test'
require 'integration/integration_helper_test'

class PostIntegrationTest < ActionDispatch::IntegrationTest

错误

  1) Error:
PostIntegrationTest#test_should_create_post:
NoMethodError: undefined method `login' for #<PostIntegrationTest:0x3da81d0>
    test/integration/post_integration_test.rb:20:in `block in <class:PostIntegrationTest>'

代码,post_integration_test.rb

require 'test_helper'
# require 'integration_helper_test'
#require 'integration/integration_helper_test'

class PostIntegrationTest < ActionDispatch::IntegrationTest
  include IntegrationHelperTest

错误

$ rake
rake aborted!
wrong argument type Class (expected Module)
C:/Users/Chloe/workspace/SeenIt/test/integration/post_integration_test.rb:6:in `include'
C:/Users/Chloe/workspace/SeenIt/test/integration/post_integration_test.rb:6:in `<class:PostIntegrationTest>'
C:/Users/Chloe/workspace/SeenIt/test/integration/post_integration_test.rb:5:in `<top (required)>'
Tasks: TOP => test:run => test:integration

test_helper.rb

ENV["RAILS_ENV"] ||= "test"
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'

class ActiveSupport::TestCase
  ActiveRecord::Migration.check_pending!

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 integration-testing


    【解决方案1】:

    name_helper_test.rb 文件不用于通用代码,这些文件用于您的助手的测试用例。

    根据以 _test.rb 结尾的Rails Testing Guide 文件用于测试用例,您应该在该文件中编写测试,因为 rake 任务从 _test.rb 检测那是测试的文件ID。因此,如果您想添加一些通用代码,test_helper.rb 可以满足您的需求。您甚至可以为辅助方法定义自己的文件,有关常见测试代码的进一步指导请阅读A Guide for Writing Maintainable Rails Tests

    注意:上述答案中的问题是您在类之外的 test_helper.rb 中编写代码,并且由于使用require,所有方法都可以在其他文件中使用,因为它们都需要 test_helper.rb

    【讨论】:

      【解决方案2】:

      选择一个:

      module IntegrationHelperTest 
        # ...
      end
      
      require 'test_helper'
      require 'integration/integration_helper_test'
      class PostIntegrationTest < ActionDispatch::IntegrationTest
        include IntegrationHelperTest
        # ...
      end
      

      class IntegrationHelperTest < ActionDispatch::IntegrationTest
        # ...
      end
      
      require 'test_helper'
      require 'integration/integration_helper_test'
      class PostIntegrationTest < IntegrationHelperTest
        # ..
      end
      

      【讨论】:

      • module IntegrationHelper 工作了!但是为什么test_helper.rbclass ActiveSupport::TestCase 时可以工作?集成测试中可以神奇地使用其中的任何辅助方法。
      • 不知道我明白了。您只能包含/扩展模块,并且只能从类继承。
      • 自动生成test/test_helper.rb。当你把一个方法放在那里时,它会自动用于所有测试。