我有一些单元测试试图测试我的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
block in'bin / rails test test / services / my_test.rb:88
我试了很多但我找不到问题所在。
我遇到的问题是我使用的图书馆有一个同名的班级。
所以我的Unittest正在调用另一个类,而不是我定义的类,为了解决这个问题,我补充说:
在测试类的标题中加载'ClassToBeTested.rb'。
更简洁的方法可能是在模块中定义ClassToBeTested并使用命名空间来调用它。