未找到Unittest中的静态方法 - NoMethodError - Ruby

时间:2017-12-17 11:40:27

标签: ruby-on-rails ruby unit-testing integration-testing

我有一些单元测试试图测试我的API:

class ClassToBeTested

def self.something_first
    # Do Something
  end

  def self.something_second
    # Do Something
  end
end

我在测试类中调用它们,然后如下:

class MyTest < ActionDispatch::IntegrationTest

  test 'should get something_first' do
    assert ClassToBeTested.something_first
  end

  test 'should get something_second' do
    assert ClassToBeTested.something_second
  end
end

最终会抛出以下错误:

  

错误:MyTest#test_should_get_something_first:NoMethodError:   未定义的方法something_first' for ClassToBeTested test/services/my_test.rb:89:in阻止在&#39;

中      

bin / rails test test / services / my_test.rb:88

我尝试了很多,但我无法找到问题所在。

1 个答案:

答案 0 :(得分:0)

我遇到的问题是我使用的图书馆有一个同名的课程。

所以我的Unittest正在调用另一个类而不是我定义的类,为了解决这个问题我添加了:

加载&#39; ClassToBeTested.rb&#39;在测试类的标题中。

更简洁的方法可能是在模块中定义ClassToBeTested并使用命名空间来调用它。

相关问题