如何暂时“class_eval”为了测试?
是否可以暂时将某些方法应用到一个类中进行测试?我希望能够根据多种应用方式来运行规范。虽然我可以制作一堆具有不同设置的装置,但我发现在测试中仅 class_eval
模型更容易。例如:
describe "some context"
before do
Page.class_eval do
my_applying_method :some => :option
end
end
it "should..."
end
然后在另一个上下文块中:
describe "another context without the gem applied"
before do
Page.class_eval do
# nothing here since I want to page to be as is
end
end
it "should do something else..."
end
但是最后一个上下文块的问题是它有一个修改过的类(在上面的上下文块中修改)。 是否可以在class_eval
之后重置课程?如何?
谢谢!
Is it possible to temporarily apply certain methods to a class for tests? I want to be able to run specs depending on many ways to apply it. While I could make a bunch of fixtures with different settings, I find it easier to just class_eval
the model in the tests. For example:
describe "some context"
before do
Page.class_eval do
my_applying_method :some => :option
end
end
it "should..."
end
Then in another context block:
describe "another context without the gem applied"
before do
Page.class_eval do
# nothing here since I want to page to be as is
end
end
it "should do something else..."
end
But the problem with the last context block is that it has a modified class (modified in the context block above). Is it possible to reset a class after class_eval
? How?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我希望有更好的方法来做到这一点,但您可以使用这个(并且在
Foo = Foo_old
行有一个警告):I hope there is a better way to do that but you can use this(and it's with a warning at the
Foo = Foo_old
line):您还没有阐明如何修改该类。
remix 库允许您暂时包含模块并在以后正确地取消包含它。
一般来说,复制类并测试副本可能是最安全的:
You haven't clarified how you are modifying the class.
The remix library allows you to temporarily include a module and properly uninclude it later.
In general, it's probably safest to just duplicate the class and test the duplicate: