Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何使助手方法在Rails集成测试中可用?_Ruby On Rails_Ruby_Ruby On Rails 4_Testing - Fatal编程技术网

Ruby on rails 如何使助手方法在Rails集成测试中可用?

Ruby on rails 如何使助手方法在Rails集成测试中可用?,ruby-on-rails,ruby,ruby-on-rails-4,testing,Ruby On Rails,Ruby,Ruby On Rails 4,Testing,我在app/helpers/sessions\u helper.rb中有一个助手文件,其中包含一个方法my\u preference,该方法返回当前登录用户的首选项。我希望能够在集成测试中使用该方法。例如,我可以在测试中使用get user\u path(我的首选项) 在其他文章中,我读到这一点是可能的,在测试文件中包括requiresessions\u helper,但我仍然得到错误namererror:undefined local variable或method'my\u preferen

我在app/helpers/sessions\u helper.rb中有一个助手文件,其中包含一个方法
my\u preference
,该方法返回当前登录用户的首选项。我希望能够在集成测试中使用该方法。例如,我可以在测试中使用
get user\u path(我的首选项)

在其他文章中,我读到这一点是可能的,在测试文件中包括
requiresessions\u helper
,但我仍然得到错误
namererror:undefined local variable或method'my\u preference'
。我做错了什么

require 'test_helper'
require 'sessions_helper'

class PreferencesTest < ActionDispatch::IntegrationTest

  test "my test" do
    ...
    get user_path(my_preference)
  end

end
需要“测试助手”
需要“会话\u助手”
类首选项测试
您的错误信息显示:

NameError: undefined local variable or method 'my_preference'
这意味着您无权访问
my_preference
方法。要使该模块在课堂上可用,您必须
模块包含在课堂中

您必须在
首选项测试
类中包含模块:
会话助手

include SessionsHelper
然后,实例方法
my_preference
将可供您在测试中使用

所以,你想做:

require 'test_helper'
require 'sessions_helper'


class PreferencesTest < ActionDispatch::IntegrationTest

  include SessionsHelper

  test "my test" do
    ...
    get user_path(my_preference)
  end

end
需要“测试助手”
需要“会话\u助手”
类首选项测试
如果有人想在所有测试中使用特定的帮助器方法,可以在
test\u helper.rb
文件中包含帮助器模块:

class ActiveSupport::TestCase 
 ...
 include SessionsHelper
end

谢谢,行得通!如果我省略了
require'sessions\u helper'
,它现在似乎也起作用了。这有意义吗?
require'sessions\u helper'
有必要吗?是的,如果它位于
app/helpers
目录下,请告诉我,您不应该需要它!