2

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!

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

Comments

4

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

3 Comments

Wow module IntegrationHelper worked! But why does test_helper.rb work when it's a class ActiveSupport::TestCase? Any helper method in there is magically available in the integration tests.
Not sure I get it. You can only include/extend modules and only inherit from classes.
It's automatically generated test/test_helper.rb. When you put a method in there, it is automatically available to all tests.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.