简体   繁体   English

如何在控制台中测试Rails类功能?

[英]How to test a Rails Class function in the console?

I'm trying to test some functions in a Rails Class in the rails console. 我正在尝试在rails控制台的Rails类中测试某些功能。

class Model
  def function1
    input_1 = ...
    input_2 = ...
    function1_hash = [ ]
    ...
    m = function2(input_1,input_2)
    function1_hash += m
  end

  def function2(input_1,input_2)
    input_1.each do |value|
      function2_hash << value
    end

    temp = input_2.anotherClassFunctionCall()
    function2_hash << temp
  end
end

So in Rails console, I'm trying to see if these functions are working properly or not but I keep running into the following error. 因此,在Rails控制台中,我试图查看这些功能是否正常运行,但我一直遇到以下错误。

# Rails console
> input_1 = ...
> input_2 = ...
> Model.function2(input_1,input_2)
NoMethodError: undefined method `function2' for Model:Class

Ultimately, I need to sanity check function1 (which is definitely failing in the greater workflow), but can't seem to even recognize the functions that it depends on within the same class. 最终,我需要检查一下function1(在更大的工作流程中肯定是失败的),但似乎甚至无法识别它依赖于同一类中的功能。

BTW, the entire rails is uploaded as a Docker file in the AWS ELB instance.And I'm using eb ssh to access rails and perform above tests. 顺便说一句,整个rails作为Docker文件上传到AWS ELB实例中。我正在使用eb ssh访问rails并执行上述测试。

Your function2 is an instance method. 您的function2是一个实例方法。 You need to call it on instance of Model class like: 您需要在Model类的实例上调用它,例如:

Model.new.function2(input_1,input_2)

Happy Coding! 编码愉快!

You can make your method a class method 您可以使您的方法成为类方法

Change: 更改:

def function2(input_1,input_2)

to: 至:

def self.function2(input_1,input_2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM