Ruby on rails 未定义的方法`记住';对于#<;应用程序帮助测试:0x000000075bf4d0>;
下面是我提到的教程 当我使用给定命令执行测试时: bundle exec rake test=test/helpers/sessions\u helper\u test.rb 我遇到以下错误: 1) 错误: ApplicationHelperTest#test(测试)当前(用户)错误时返回(无): NoMethodError:未定义的方法Ruby on rails 未定义的方法`记住';对于#<;应用程序帮助测试:0x000000075bf4d0>;,ruby-on-rails,ruby,Ruby On Rails,Ruby,下面是我提到的教程 当我使用给定命令执行测试时: bundle exec rake test=test/helpers/sessions\u helper\u test.rb 我遇到以下错误: 1) 错误: ApplicationHelperTest#test(测试)当前(用户)错误时返回(无): NoMethodError:未定义的方法记住'for# test/helpers/sessions\u helper\u test.rb:7:insetup' 2) 错误: ApplicationHe
记住'for#
test/helpers/sessions\u helper\u test.rb:7:in
setup'
2) 错误:
ApplicationHelperTest#test(测试)当前(用户)返回(右)用户(带)会话(无):
NoMethodError:未定义的方法记住'for#
test/helpers/sessions\u helper\u test.rb:7:in
setup'
我在/apps/helpers/session_helper.rd中定义了一个helper类方法。 --记住方法是这个类的一部分。。但是/test/helpers/sessions\u helper\u测试仍然无法找到该方法 /test/helpers/sessions\u helper\u test.rb文件的代码为
require 'test_helper'
class ApplicationHelperTest < ActionView::TestCase
def setup
@user = users(:michael)
remember(@user)
end
test "Current_user returns right user with session is nill" do
assert_equal @user, current_user
assert is_logged_in?
end
test "current_user returns nil when remember digest is wrong" do
@user.update_attribute(:remember_digest, User.digest(User.new_token))
assert_nil current_user
end
end
Users.yml文件是:
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
michael:
name: Michael Example
email: michael@example.com
password_digest: <%= User.digest('password') %>
archer:
name: Sterling Archer
email: duchess@example.gov
password_digest: <%= User.digest('password') %>
#阅读以下网站上的固定装置http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
迈克尔:
姓名:Michael Example
电邮:michael@example.com
密码摘要:
弓箭手:
姓名:斯特林弓箭手
电邮:duchess@example.gov
密码摘要:
-----------------找到解决办法了吗-------------------
您需要将HelperModule包含到测试模块中。。
test/helpers/sessions\u helper\u test.rb的修复代码如下
require 'test_helper'
class ApplicationHelperTest < ActionView::TestCase
include SessionsHelper # This line is added to code.
def setup
@user = users(:michael)
remember(@user)
end
test "Current_user returns right user with session is nill" do
assert_equal @user, current_user
assert is_logged_in?
end
test "current_user returns nil when remember digest is wrong" do
@user.update_attribute(:remember_digest, User.digest(User.new_token))
assert_nil current_user
end
end
需要“测试助手”
类ApplicationHelperTest
也遵循本教程,并且有类似的问题。通过从以下位置更新类,可以避免使用include:
class ApplicationHelperTest < ActionView::TestCase
class ApplicationHelperTest
致:
class SessionHelperTest
粘贴记忆方法。似乎不存在。您可以发布sessions\u helper的内容吗?谢谢astreal和Jorge de los Santos。。我找到了解决办法。。将SessionHelper模块包括到类应用程序hepertest中的那一刻。。这东西有用你能帮我解释一下为什么我需要特别包括这些文件吗ruby test是否不允许从其各自的助手类访问方法?@One将模块包含在ApplicationHelperTest
中,将其纳入范围。模块可能是一个奇怪的野兽,例如你可以做modulesessionhelp;延伸自我;你的方法;结束
然后用SessionHelper调用它。记住(@user),但使用包含可以直接调用该方法,就好像您已将其键入到ApplicationHelperTest
本身一样。
class ApplicationHelperTest < ActionView::TestCase
class SessionsHelperTest < ActionView::TestCase