Ruby: Alter class static method in a code block

Posted by Phuong Nguy?n on Stack Overflow See other posts from Stack Overflow or by Phuong Nguy?n
Published on 2010-06-10T11:47:47Z Indexed on 2010/06/10 11:53 UTC
Read the original article Hit count: 213

Given the Thread class with it current method. Now inside a test, I want to do this:

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

Basically, I want to alter the method of a class inside a test method and recover it after that. I know it sound complex for another language, like Java & C# (in Java, only powerful mock framework can do it). But it's ruby and I hope such nasty stuff would be available

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about mocking