加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby – 尝试访问包含模块中定义的方法时的NoMethodError

发布时间:2020-12-17 03:30:42 所属栏目:百科 来源:网络整理
导读:我正试图从我的一个规范助手中的模块中访问一个方法 我将模块包含在测试助手中 module Support class RestHelper include Rest::Rest def create_rest_client_for_ifa # Call method from module create_rest_client(uname,pword) end endend 但是当我运行我
我正试图从我的一个规范助手中的模块中访问一个方法

我将模块包含在测试助手中

module Support
  class RestHelper
    include Rest::Rest

    def create_rest_client_for_ifa
      # Call method from module
      create_rest_client(uname,pword)
    end
  end
end

但是当我运行我的规范时,我不断收到NoMethodError:

Failure/Error: @rest_client = Support::RestHelper.create_rest_client_for_ifa
 NoMethodError:
   undefined method `create_rest_client' for Support::RestHelper:Class

这是我的模块代码:

module Rest
  module Rest
    .
    .
    def create_rest_client(uname,pword)
      # code
    end
    .
    .
  end
end

当我在rails控制台中测试它时似乎工作正常

$RAILS_ENV=test rails c
irb> include Rest::Rest
=> Object
irb> create_rest_client(uname,pword)

我错过了什么?为什么我不能从测试助手访问该方法?

任何帮助都感激不尽.

解决方法

我记得,include将模块方法添加为实例方法,extend将它们添加为类方法.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读