How do I write a helper for an integration test in

2019-07-09 00:53发布

问题:

How do I write an integration test helper that is used amongst several integration tests? I've tried the following with the following errors. I'm considering making a base class and extending that, but I don't understand how 'test_helper' is working! I can't put the helper methods in test_helper because they use special integration helpers like post_with_redirect.

LS

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

Code, integration_helper_test.rb

class IntegrationHelperTest < ActionDispatch::IntegrationTest

  def login(user)
    ...

Code, post_integration_test.rb

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

class PostIntegrationTest < ActionDispatch::IntegrationTest
  # include IntegrationHelperTest

Error

$ 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

Code, post_integration_test.rb

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

class PostIntegrationTest < ActionDispatch::IntegrationTest

Error

  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>'

Code, post_integration_test.rb

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

class PostIntegrationTest < ActionDispatch::IntegrationTest
  include IntegrationHelperTest

Error

$ 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!

回答1:

Pick one:

module IntegrationHelperTest 
  # ...
end

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

or

class IntegrationHelperTest < ActionDispatch::IntegrationTest
  # ...
end

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


回答2:

name_helper_test.rb files are not for common code, these are for the testcases for your helpers.

According to Rails Testing Guide file that ends at _test.rb are for test cases, you should write tests in that file, because rake task detects from _test.rb that is file id for tests. So if you want to add some common code, test_helper.rb is there for you. You can even define your own files for helpers methods, for further guide about common test code read A Guide for Writing Maintainable Rails Tests.

Note: Questions in the comments of above answers is that you write your code in test_helper.rb outside of a class and due to use of require all methods are available in other files, because they all require test_helper.rb.