我正准备在我的rails 3.2.16应用程序上进行一些集成测试,我想,在我的用户场景中,我有几个调用,我在很多测试中重复,所以我想通过放置它们来干它们在一个单独的共同模块中,
例如我创建了/test/integration/my_test_helpers.rb
:
require 'test_helper'
module MyTestHelper
def login_user(email, password, stay = 0)
login_data = {
email: email,
password: password,
remember_me: stay
}
post "/users/sign_in", user: login_data
assert_redirected_to :user_root
end
end
并尝试在我的集成测试中使用它:
require 'test_helper'
require "./my_test_helpers.rb"
class CreateCommentTest < ActionDispatch::IntegrationTest
setup do
@user = users(:user1)
end
test "create comment" do
login_user @user.email, "password", 1
end
end
我得到例外:
`require': cannot load such file -- ./my_test_helpers.rb (LoadError)
如何加载模块?让MyTestHelpers
成为一个模块是对的吗?
答案 0 :(得分:1)
您应该将助手放在支持文件夹(test/support/my_test_helpers.rb
或其他内容)中并在test_helper.rb
中加载模块:
ENV["RAILS_ENV"] ||= "test"
require File.expand_path("../../config/environment", __FILE__)
require "rails/test_help"
require_relative "./support/my_test_helpers"
require "minitest/rails"
class ActiveSupport::TestCase
ActiveRecord::Migration.check_pending!
fixtures :all
# Add more helper methods to be used by all tests here...
end
不记得你的模块include
:
require 'test_helper'
class CreateCommentTest < ActionDispatch::IntegrationTest
include MyTestHelper
setup do
@user = users(:user1)
end
test "create comment" do
login_user @user.email, "password", 1
end
end