Tag: 模拟静态方法

Ruby:在代码块中更改类静态方法

给出Thread类的当前方法。 现在在测试中,我想这样做: def test_alter_current_thread Thread.current = a_stubbed_method # do something that involve the work of Thread.current Thread.current = default_thread_current end 基本上,我想在测试方法中更改类的方法,然后在之后恢复它。 我知道它对于另一种语言来说听起来很复杂,比如Java和C#(在Java中,只有强大的模拟框架才能做到)。 但它是ruby,我希望这些令人讨厌的东西可用