I have a module Datetools
in lib/utils/datetools.rb
:
module Datetools
def hello
'world'
end
end
I'd like to test it with a class called DatetoolsTest
in test/utils/datetools_test.rb
:
import Datetools
class DatetoolsTest < TestCase
test 'hello world' do
assert Datetools.hello() == 'world'
end
end
When I run:
rails t test/utils/datetools_test.rb
I'm getting the error:
uninitialized constant Datetools (NameError)
How can I get hold of my Datetools
module in the test case?
Versions: Ruby 3.3.5, Rails 7.1.4
You seem very confused here.
lib
is not on the autoload paths. So to load it you need to do one of the following:
require Rails.root.join('lib/utils/datetools')
app/utils
instead of lib as it and all it's subfolders are autoloading roots.However even if you load the code you're still going to get undefined method 'hello' for module Datetools (NoMethodError)
. If you want the method to be callable as a module method you need to use def self.hello
or the module_function
method.
If what you actually want to do is to test instance methods of the module then a better way is to create a stand in class in your test:
class DatetoolsTest < TestCase
# You could also use Class.new to create an anonymous class instead
class DummyClass
include Datetools
end
test 'hello world' do
assert_equal DummyClass.new.hello, 'world'
end
end
Or include the module into the test class:
class DatetoolsTest < TestCase
include Datetools
test 'hello world' do
assert_equal hello, 'world'
end
end