尝试访问包含的模块中定义的方法时出现NoMethodError [英] NoMethodError when trying to access method defined in included module

查看:101
本文介绍了尝试访问包含的模块中定义的方法时出现NoMethodError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从我的一个规范助手中的模块访问方法

I'm trying to access a method from a module in one of my spec helpers

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

I include the module in the test helper

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:

But I keep getting a NoMethodError when I run my spec:

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控制台中对其进行测试时,它似乎可以正常工作

It seems to work fine when I test it in the rails console

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

我想念什么?为什么我不能从测试助手中访问该方法?

What am I missing? Why can't I access the method from the test helper?

任何帮助将不胜感激.

推荐答案

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

As I remember, include adds module methods as instance methods, extend adds them as class methods.

这篇关于尝试访问包含的模块中定义的方法时出现NoMethodError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆