我将我的模块移动到
app/lib/parsers
文件夹。
模型类响应模块方法,但是当我尝试
rspec
I have issue
Failure/Error: include MainModule::Submodule
NameError:
uninitialized constant ModelName::MainModule
module in lib/parsers looks like this
Module Parsers
Module Parser1
def foo
end
end
Module Parser2
def bar
end
end
end
第一个模型通过这种方式包括第一个解析器
Class Model1 < ApplicationRecord
include Parsers::Parser1
end
和第二个以同样的方式在 Rspec 中要求该模块的最佳方法是什么?
要求该模块的最佳方法是什么
使用 rails 的自动加载机制。把你的Parsers::Parser1
toapp/lib/parsers/parser1.rb
和Parsers::Parser2
to app/lib/parsers/parser2.rb
。查看模块的全名如何反映它的存储位置?这就是 rails 能够找到它的方式。
或者您可以明确要求该文件
# my_spec.rb
require_dependency Rails.root.join('app', 'lib', 'parsers')
RSpec.describe ...
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句