断言红宝石模块扩展了另一个模块
我有两个模块A& B,我将A扩展到B中,以便使用B中的A方法作为类方法:
module A
def foo
end
end
module B
extend A
end
B.Foo
我想编写一个测试以断言模块B扩展A。方法,但我认为这是一个好主意。无论如何,是否有声称一个模块扩展了另一个模块?我可以使用响应_to?方法但我必须循环循环扩展模块中的所有方法,我认为这不是一个很好的设计。再次感谢,和平。
I have two modules A & B, I am extending A into B in order to use the methods of A in B as class methods:
module A
def foo
end
end
module B
extend A
end
B.Foo
I'd like to write a test to assert that module B Extends A. Currently Ruby does not implement an extends? method but I think that would be a great idea. Is there anyway to assert that a module extends another module? I could use the responds_to? method but I'd have to loop over all of the methods in the extending module and that in my opinion is not a great design. Thanks again, peace.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我同意 @Spickermann的评论,即这似乎不是一个有用的测试,也就是说:
给您想要的东西。
I agree with @spickermann's comment that it doesn't seem like a useful test, that said:
Gives you what you want.
从本质上讲,只是“插入”
m
作为c
的超级阶级。或者,更确切地说,它创建一个来自m
的class ,使得将其包括c
的超类和的旧超级类别C
包括类的超级类。此外,
本质上与
singleton class
c
的祖先链中插入m
。由于
c
是其Singleton类的实例,m
是Singleton类的超级类,c
是m
m
c
/代码>。这意味着,您需要做的只是
其他答案和评论中提到的,您 不关心
b
是的实例
。您做的关心是,当您致电b.foo
时,您会得到您期望的行为。因此,断言 。essentially just "inserts"
M
into the ancestry chain as the superclass ofC
. Or, more precisely, it creates an include class fromM
, makes that include class the superclass ofC
and the old superclass ofC
the superclass of the include class.Furthermore,
is essentially just the same as
i.e. it inserts
M
into the the ancestry chain of the singleton class ofC
.Since
C
is an instance of its singleton class andM
is the superclass of the singleton class,C
is an instance ofM
.What this means is that all you need to do is
However, as mentioned in other answers and comments, you don't really care that
B
is an instance ofA
. What you do care about is that when you callB.foo
, you get the behavior that you expect. So, assert that.