[英]make a helper for my methods for integration testing in rails 3
I'm preparing some integration tests on my rails 3.2.16 application, I figured that, in my user scenarios, I have several calls that I repeat over many tests, so I would like to DRY them up, by placing them in a separate common module, 我正在我的Rails 3.2.16应用程序上准备一些集成测试,我发现在我的用户场景中,我有多次重复许多测试的调用,因此我想通过将它们放在单独的位置进行干燥通用模块
for example I have created /test/integration/my_test_helpers.rb
: 例如,我创建了/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
and tried to use it in my integration test: 并尝试在我的集成测试中使用它:
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
I get exception: 我得到异常:
`require': cannot load such file -- ./my_test_helpers.rb (LoadError)
How can I load the module? 如何加载模块? is it right to make MyTestHelpers
a module? 将MyTestHelpers
为模块是否正确?
You should put your helper in support folder( test/support/my_test_helpers.rb
, or something) and load module in test_helper.rb
: 您应该将帮助程序放在支持文件夹( 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
Do not remember include
your module: 不记得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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.