RSPEC说'未定义的方法'在包含另一个模块的模块上运行时
我的情况是当前:
module Base
module A
include Base::B
end
end
module Base
module B
module_function
def my_method
...
end
end
end
然后我的测试:
Rspec.describe Base::A do
describe '.my_method' do
it 'does something'
...
end
end
end
问题:
nomethoderror。未定义的方法'my_method'for Base :: a:模块
我缺少什么?提前致谢!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
module_function
让我们可以通过定义单例方法在模块上调用我们的方法( my_method 的完整单独副本):它也将
my_method
转换为一个私有方法,这是您在“包括”或“扩展”时获得的方法:如果
base :: b
包含在base :: a
模块中:因为我们无法在模块中初始化一个实例,我们可以通过将
base :: A
使用self
扩展到类方法。或扩展
base :: b
从开始:module_function
执行等效:https://ruby-doc.org/core-core-core-3.1.2/module.html#metml#method-method-i-i-i-module_function
https> https:/// Ruby-doc.org/core-3.1.2/module.html#method-i-include
https://ruby-doc.org/core-3.1.2/Object.html#method-i-extend< /em>
module_function
lets us call our method on the module by defining a singleton method (a full separate copy of my_method):It also turns
my_method
into a private method, this is the method you get when "including" or "extending":Same rules apply if
Base::B
is included inBase::A
module:Since we can't initialize an instance out of the module, we can bump the instance method to a class method by extending
Base::A
withself
.or extend
Base::B
from the start:module_function
does the equivalent of this:https://ruby-doc.org/core-3.1.2/Module.html#method-i-module_function
https://ruby-doc.org/core-3.1.2/Module.html#method-i-include
https://ruby-doc.org/core-3.1.2/Object.html#method-i-extend