本文介绍了如何为Rails中的集成测试编写帮助程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何编写在多个集成测试中使用的集成测试助手?我尝试了以下错误.我正在考虑制作一个基类并进行扩展,但是我不明白'test_helper'是如何工作的!我无法将帮助程序方法放在test_helper中,因为它们使用特殊的集成帮助程序,例如 post_with_redirect .

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

推荐答案

选择一个:

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

这篇关于如何为Rails中的集成测试编写帮助程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 18:53