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

问题描述 投票:0回答:1

我有一些单元测试试图测试我的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:inblock in'

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

我试了很多但我找不到问题所在。

ruby-on-rails ruby unit-testing integration-testing
1个回答
0
投票

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

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

在测试类的标题中加载'ClassToBeTested.rb'。

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

© www.soinside.com 2019 - 2024. All rights reserved.