0

我想在Rails控制台中的Rails类中测试一些函数。如何在控制台中测试Rails Class功能?

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 

所以在Rails的控制台,我想看看这些功能工作正常与否,但我一直运行到下面的错误。

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

最后,我需要完整性检查功能1(这肯定是在更大的工作流程失败的),但似乎无法甚至承认,这取决于同一个类中的功能。

BTW,整个轨道上载作为AWS ELB instance.And泊坞窗文件我用EB SSH进入轨道并进行上述试验。

回答

2

您的function2是一种实例方法。你需要像调用它实例Model类:

Model.new.function2(input_1,input_2) 

编码快乐!

+0

感谢您的输入。在function1之前有一个单独的_ ** def init ** _函数,当我使用你的时: Model.new.function2(input_1,input_2) 我得到了嵌入在_ ** def中的同样的异常错误function1 ** _当我运行一般_ ** Model.create!(input_variable:some_varialbe)** _( - >这是我需要工作的): rescue Exception => e run_errors <<“错误...“ self.state =:错误 throw”错误正在运行此列表...“ 如何检查以查看引发错误的语句是什么? – Josh

0

你可以让你的方法一类方法

变化:

def function2(input_1,input_2) 

到:

def self.function2(input_1,input_2)